C bitfields memory usage

老子叫甜甜 提交于 2019-12-04 10:00:14
Himadri Choudhury

The problem is some padding is being added by the compiler for efficiency reasons.

This behavior can be overridden.

For how to do this with gcc see forcing alignment in GCC

For how to do this with visual c++ see: forcing alignment in Visual C++

Your structure is 9 bytes long. Compiler pads it to 16 bytes to be more cache friendly. This can be turned off (I do not recommend it in general) using compiler-specific directives/keywords. See data structure alignment.

You can use the gcc specific forced alignment :

typedef struct{
    unsigned n1 : 12;
    unsigned n2 : 12;
    unsigned n3 : 12;
    unsigned n4 :  1;
    unsigned n5 : 35;
} data __attribute__((__packed__));

Read: http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Type-Attributes.html

This would work extremely well on a 36-bit computer. You forgot to tell us if that is what you have got...

On a more common 32-bit machine, using a 9 byte alignment would be very difficult to implement. If you create an array of these structs, you would need different code to access the fields in an object with an address modulo 0 and one with an address modulo 9.

The 12 bits of the first three fields would have to be collected from different unsigneds, and differently depending on the address.

The packing directives in the the other answers are unlikely to work here, unless you have hardware with bit addressing (or a 36-bit CPU).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!