Pelles C > General discussions

Can I Relax Alignment on struct members?

(1/1)

PabloMack:
I have created a struct that uses types of odd byte sizes. Seems that the compiler wants to pad every member to 8 bytes. Can I relax the alignment constraints on struct members so that the size of the structure will always be the sum of the the declared sizes of all its members with no padding? It is perfectly fine for them to be misaligned because I will explicitly pad with values to bring the data structure to the size I want it to be which will probably be 128 bytes.

If I can't do it then I will have to resort to a lot of casts and explicit offset values to bypass the error checking straight jacket.

frankie:
use packing pragmas.


--- Code: ---#pragma pack(1)    //pack structures on byte boundary
typedef struct
{
    char a[3];
    short b;
} PackedStruct;
#pragma pack()    //restore standard packing

PackedStruct my_struct;
printf("%u bytes\n", sizeof(my_struct));

--- End code ---

Will print 5 bytes.

PabloMack:
Thanks. I've never used pragmas in my entire 34 years of writing tons of C code
for many different compilers.  But I guess this will be the place to start.

Navigation

[0] Message Index

Go to full version