user3004790
user3004790

Reputation: 778

calculating the size of structure

I was going through the concept of structure member alignment and structure padding concept when I found that the size of this struct ...

struct node{
  int b;
  char a;
 };

... is 8 bytes (i.e. it contains 3 bytes of padding) whereas the size of this struct ...

struct node{
  char a;
};

... is only 1 byte. Why is there no padding in the latter?

Upvotes: 0

Views: 179

Answers (3)

shauryachats
shauryachats

Reputation: 10385

Data structure alignment.

Described wonderfully here.

Suppose you have this structure:

struct  {
    char a[3];
    short int b;
    long int c;
    char d[3];
    };

Now, you might think that it ought to be possible to pack this structure into memory like this:

+-------+-------+-------+-------+
|           a           |   b   |
+-------+-------+-------+-------+
|   b   |           c           |
+-------+-------+-------+-------+
|   c   |           d           |
+-------+-------+-------+-------+

But it's much, much easier on the processor if the compiler arranges it like this:

+-------+-------+-------+
|           a           |
+-------+-------+-------+
|       b       |
+-------+-------+-------+-------+
|               c               |
+-------+-------+-------+-------+
|           d           |
+-------+-------+-------+

In the ''packed'' version, notice how it's at least a little bit hard for you and me to see how the b and c fields wrap around? In a nutshell, it's hard for the processor, too. Therefore, most compilers will ''pad'' the structure (as if with extra, invisible fields) like this:

+-------+-------+-------+-------+
|           a           | pad1  |
+-------+-------+-------+-------+
|       b       |     pad2      |
+-------+-------+-------+-------+
|               c               |
+-------+-------+-------+-------+
|           d           | pad3  |
+-------+-------+-------+-------+

This post explained how padding is done, not why it is done. Please refer haccks' answer for why it is done.

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180201

The padding and alignment of members of a struct are completely at the discretion of the compiler, except that there cannot be any padding before the first member. With that said, compilers normally choose padding to serve alignment purposes, so that, in particular,

  1. it is possible for all the members of the same struct to be optimally aligned at the same time, and
  2. in an array of structs, if the members of the first array element are aligned optimally then so will be the members of all the others.

Subject to those and similar requirements, compilers typically choose the least amount of padding necessary (ideally no padding at all). If different member types have different alignment requirements, then the above considerations require struct representations to be padded to a multiple of the size of the member with the most stringent alignment requirement. They may also require padding between members, depending on the types of the members and their order.

Upvotes: 1

haccks
haccks

Reputation: 106012

Remember theses two rules for padding:

  1. Padding is only inserted when a structure member is followed by a member with a larger alignment requirement or at the end of the structure.
  2. The last member is padded with the number of bytes required so that the total size of the structure should be a multiple of the largest alignment of any structure member.

Member a in second structure is not followed by any member or member with larger alignment and it is also the largest alignment member which means--No Padding.

Read this answer for more detailed explanation.

Upvotes: 7

Related Questions