Packing a ctypes struct containing bitfields.

Karthik karthik301176 at gmail.com
Thu Jun 18 06:30:35 EDT 2009


Hello Everybody,

I'm trying to create a packed structure in ctypes (with one 64-bit
element that is bitfielded to 48 bits),
unsuccessfully:

===================================
from ctypes import *

class foo (Structure):
    _pack_ = 1
    _fields_ = [
        ("bar",    c_ulonglong, 48),
    ]

print("sizeof(foo) = %d" % sizeof(foo))
===================================
I'm expecting that this should print 6 - however, on my box, it prints
8.

The following piece of C code, when compiled and run, prints 6, which
is correct.
===================================
struct foo {
    unsigned long long bar: 48;
};

printf("sizeof(foo) = %d", sizeof(foo));
===================================

So... what am I doing wrong?

Thanks,
Karthik.



More information about the Python-list mailing list