Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding in structures in C

This is an interview question. Till now, I used to think such questions were purely compiler dependent and shouldn't worry me, but now, I am rather curious about it.

Suppose you are given two structures as:

struct A {     int* a;     char b;    }   

and ,

struct B {     char a;     int* b;   }   

So which one would you prefer and why? My answer went like this (though I was somewhat shooting in the dark) that the first structure should be preferred since the compiler allocates space for a structure in some multiples of the word size (which is the size of the pointer - 4 bytes on 32 bit machines and 8 bytes on 64 bit ones). So, for both the structures the compiler would allocate 8 bytes(assuming its a 32 bit machine). But, in the first case, the padding would be done after all my variables(i.e. after a and b). So even if by some chance, b gets some value that overflows and destroys my next padded bytes, but my a is still safe.

He didn't seemed much pleased and asked for one disadvantage of the first structure over the second. I didn't have much to say. :D

Please help me with the answers.

like image 506
letsc Avatar asked Aug 06 '11 17:08

letsc


People also ask

What is padding in structure in C?

Structure Padding in C:The structure padding is automatically done by the compiler to make sure all its members are byte aligned. The size of the below structure is 16 bytes due to structure padding: Struct dummy { Char ch; Int num; Double temp; }

Why struct padding is needed?

'Why is structure padding required?' The answer to that lies in how a CPU accesses memory. Typically a CPU has alignment constraints, e.g. a CPU will access one word at a time, or a CPU will require data to be 16byte aligned, etc.

How can we avoid structure padding in C?

In Structure, sometimes the size of the structure is more than the size of all structures members because of structure padding. Note: But what actual size of all structure member is 13 Bytes. So here total 3 bytes are wasted. So, to avoid structure padding we can use pragma pack as well as an attribute.

What is padding and packing in C?

Depending on the CPU architecture and the compiler, a structure may occupy more space in memory than the sum of the sizes of its component members. The compiler can add padding between members or at the end of the structure, but not at the beginning. Packing overrides the default padding.


2 Answers

I don't think there's an advantage for any of this structures. There is one(!) constant in this equation. The order of the members of the struct is guaranteed to be as declared.

So in case like the following, the second structure might have an advantage, since it probably has a smaller size, but not in your example, as they will probably have the same size:

struct {     char a;     int b;     char c; } X; 

Vs.

struct {     char a;     char b;     int c; } Y; 

A little more explanation regarding comments below:

All the below is not a 100%, but the common way the structs will be constructed in 32 bits system where int is 32 bits:

Struct X:

|     |     |     |     |     |     |     |     |     |     |     |     |  char  pad    pad   pad   ---------int---------- char   pad   pad   pad   = 12 bytes 

struct Y:

|     |     |     |     |     |     |     |     |  char  char  pad   pad   ---------int----------        = 8 bytes 
like image 76
MByD Avatar answered Sep 24 '22 06:09

MByD


Some machines access data more efficiently when the values aligned to some boundary. Some require data to be aligned.

On modern 32-bit machines like the SPARC or the Intel [34]86, or any Motorola chip from the 68020 up, each data iten must usually be ``self-aligned'', beginning on an address that is a multiple of its type size. Thus, 32-bit types must begin on a 32-bit boundary, 16-bit types on a 16-bit boundary, 8-bit types may begin anywhere, struct/array/union types have the alignment of their most restrictive member.

So you could have

struct B {       char a;     /* 3 bytes of padding ? More ? */     int* b; } 

A simple rule that minimize padding in the ``self-aligned'' case (and does no harm in most others) is to order your struct members by decreasing size.

Personally I see not disadvantage with the first struct when compared to the second.

like image 33
cnicutar Avatar answered Sep 21 '22 06:09

cnicutar