Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find holes in C structs due to alignment

Is there a way in gcc or clang (or any other compiler) to spit information about whether a struct has holes (memory alignment - wise) in it ?

Thank you.

ps: If there is another way to do it, please do inform me.

like image 288
John Avatar asked Oct 06 '11 12:10

John


People also ask

Are structs 8 byte aligned?

Assuming a 64-bit machine, any instance of struct foo1 will have 8-byte alignment.

How many bytes of alignment does the struct as a whole require?

General Byte Alignment Rules Structures between 1 and 4 bytes of data should be padded so that the total structure is 4 bytes. Structures between 5 and 8 bytes of data should be padded so that the total structure is 8 bytes.

What is __ packed in C?

4.11 The __packed__ Attribute This attribute, attached to struct or union type definition, specifies that each member (other than zero-width bitfields) of the structure or union is placed to minimize the memory required. When attached to an enum definition, it indicates that the smallest integral type should be used.

What is struct padding in C?

Structure padding is a concept in C that adds the one or more empty bytes between the memory addresses to align the data in memory.


1 Answers

You can use pahole to output information about holes in structures and optionally attempt packing them.

You may want to read "Poke-a-hole and friends" and the pahole announcement for more information

like image 92
Hasturkun Avatar answered Sep 21 '22 22:09

Hasturkun