LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Prevent compiler padding structures out to WORD boundaries (https://www.linuxquestions.org/questions/programming-9/prevent-compiler-padding-structures-out-to-word-boundaries-507727/)

guestgulkan 12-05-2006 02:45 PM

Prevent compiler padding structures out to WORD boundaries
 
Hi,
If you have a structure that is an odd number of bytes (say 7 bytes), the compiler
will probably add 1 byte to pad it out to 8bytes (so the sizeof() function will return
the structure size as 8 bytes).
99.99% of the time this is ok. Is there a compiler driective I can insert in the structure definition if I want sizeof() to return the as_desgined number of bytes(in this particular example 7 bytes)?

tuxdev 12-05-2006 03:03 PM

You can use __attribute((packed)). Why do you want to do this? There really aren't very many good reasons.

guestgulkan 12-05-2006 03:38 PM

it's just that i'm messing about with usb - and at some points the devices returns data which is consecutive structures end to end - and they are not an even number of bytes each.
So my pointer arithmetic gets messed up if you get what I mean.

tuxdev 12-05-2006 03:57 PM

Okay, that's what I thought. Do you use bitfield structures? Not only does it do what you want, but it also isn't vulnerable to the various definitions of integer types.


All times are GMT -5. The time now is 07:11 AM.