Re: why sturct size different between C and Python ctypes?

"Diez B. Roggisch" <[email protected]> Mon, 22 Feb 2016 09:54:34 +0100
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
I find the results very surprising. nlmsghdr is 16 Bytes for me, which leaves ~8.5 Bytes for each of inet_diag_ret fields. And that makes little to no sense to me - even on a 64 bit system (one explanation for differing sizes, btw) there shouldn’t be > 8 bytes per entry.

Can you break down the sizeofs of various substructures in C and Python to home in on this?

Diez

> 
> 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
signature.asc (application/pgp-signature, 496 B)
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQEcBAEBCgAGBQJWyszLAAoJEMgWrvcLlYXn5KEIALX/vSA53fZod5153s4b4v66
V5aZzq69jaaxY5BjGipTH1C24liDxk6GSSdBlTvkmZuFH7QjM6i+qq6skuLwguu4
X8J1+i+GM4VMQLGyC/guz5w9NLdeKWsU9ZibpnxnVsWwf26lx2+YiOCtlOXN04E2
jt/tgj7h+jjk9EFvUH8p9o/2KLJLQMzzxtIXTarl4oP1wbdKmTFHNqR2F/fHEtw/
bjjEteAlYSxvkvM1fbuzEVL/71fFBp/DtFCLQ7SiswSPEULaCPL/Sq1CqLgdNNSB
gPPgin1cvhGbIaJNGCo3xZss1uPe6CEc80XrCw318XxFgBxhrE4Qu8rbLeNNeHE=
=ME5b
-----END PGP SIGNATURE-----