Re: why sturct size different between C and Python ctypes?
Stephen GEORGE <[email protected]> Mon, 22 Feb 2016 21:01:16 +1100
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
The reference I found to inet_diag_req online shows Inet_diag_sockid is
a structure contained by value NOT by reference, .. I took away the
POINTER and got a total of 76 bytes ( packing =1)
nlmsghdr = 16 pack=1
inet_diag_req= 60 pack=1
===================
nlmsg = 76 pack=1
Check to see if I am correct.
class inet_diag_req(ctypes.Structure):
_pack_ = ww
_fields_ = [
("idiag_family", ctypes.c_uint8), # 1 = 1
("idiag_src_len", ctypes.c_uint8), # 1 = 2
("idiag_dst_len", ctypes.c_uint8), # 1 = 3
("idiag_ext", ctypes.c_uint8), # 1 = 4
# ("id", ctypes.POINTER(Inet_diag_sockid)),# 4 = 8
("id", Inet_diag_sockid), #<<<< by value, not reference
("idiag_states", ctypes.c_uint32), # 4 = 12
("idiag_dbs", ctypes.c_uint32) # 4 = 16
] # total = 16
Steve
On 22/02/16 18:52, Yang Liu wrote:
> Hi,all
>
> I am using ctypes today and a sturct size is differet in C and Python
> ctypes code
> here is my code
>
> C:
> #include<linux/netlink.h>
> #include<linux/inet_diag.h>
>
> int main(int argc, char *argv[])
> {
> struct {
> struct nlmsghdr nlh;
> struct inet_diag_req req;
> } nlreq;
> printf("nlreq: %d\n", sizeof(nlreq));
> return 0;
> }
>
> out:
> nlreq: 76
>
>
> Python:
> class nlmsghdr(ctypes.Structure):
> _fields_ = [
> ("len", ctypes.c_uint32),
> ("type", ctypes.c_uint16),
> ("flags", ctypes.c_uint16),
> ("seq", ctypes.c_uint32),
> ("pid", ctypes.c_uint32)]
>
> class Inet_diag_sockid(ctypes.Structure):
> _fields_ = [
> ("idiag_sport", ctypes.c_uint16.__ctype_be__),
> ("idiag_dport", ctypes.c_uint16.__ctype_be__),
> ("idiag_src", ctypes.c_uint32.__ctype_be__ * 4),
> ("idiag_dst", ctypes.c_uint32.__ctype_be__ * 4),
> ("idiag_if", ctypes.c_uint32),
> ("idiag_cookie", ctypes.c_uint32 * 2),
> ]
>
> class inet_diag_req(ctypes.Structure):
> _fields_ = [
> ("idiag_family", ctypes.c_uint8),
> ("idiag_src_len", ctypes.c_uint8),
> ("idiag_dst_len", ctypes.c_uint8),
> ("idiag_ext", ctypes.c_uint8),
> ("id", ctypes.POINTER(Inet_diag_sockid)),
> ("idiag_states", ctypes.c_uint32),
> ("idiag_dbs", ctypes.c_uint32)
> ]
>
> class nlmsg(ctypes.Structure):
> _fields_ = [
> ("nlh", nlmsghdr),
> ("req", Inet_diag_req)
> ]
>
> print ctypes.sizeof(nlmsg)
>
> out:
> 24
>
>
> why the same struct size is 76 inC and 24 in ctypes ?
>
> Thanks
>
>
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
>
>
> _______________________________________________
> ctypes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/ctypes-users
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users