2

Possible Duplicate:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?

#include <stdio.h>

int main(){

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

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

printf("%d\t%d\n", sizeof(int), sizeof(char));   //Output : 4 1
printf("%d\t%d\n", sizeof(struct word1), sizeof(struct word2)); //Output: 12 8
return 0;
}

The code is available at IDEONE.

Why is the size of struct 1 (word1) greater than the size of struct 2 (word2)?

Is this a compiler problem?

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Don't use '`void main()`' and expect to be unshouted at - the correct return type for `main()` is `int`. – Jonathan Leffler Oct 24 '10 at 07:25
  • one day someone is going to claim that returning void instead of int caused a nuclear meltdown, or something. – Crashworks Oct 24 '10 at 07:27
  • OK, I'll take care of this from next time. – Chankey Pathak Oct 24 '10 at 07:32
  • @Crashworks: maybe, but not by me. It means that there is no reliable value returned to the environment, so if program A is relying on the exit status of program B, it is undefined what value A will receive from B if B returns no value. Now, if the programs are anywhere near a nuclear pile, one would assume that the code reviews and testing and coding standards all ensure that no disaster happens. If beginners learn to return a value from main(), it helps get them ready to work in more demanding environments than classroom exercises. Eventually, they should read the C standard, but not yet. – Jonathan Leffler Oct 24 '10 at 07:35
  • Gotta be one of the most duplicated on-topic question on Stack Overflow. – dmckee --- ex-moderator kitten Oct 24 '10 at 19:33
  • @Jonathan Leffler, I use `void main(void)` all the time. Saves several bytes of ROM and RAM on the AVR. – Vorac Apr 25 '13 at 09:45
  • @Vorac: if you're working in a freestanding environment, or if the hosted environment specifically supports `void main()`, then fine. However, in a general C question like this (one which doesn't stipulate a freestanding environment), then it is bad practice to use `void main()`; hence my comment. – Jonathan Leffler Apr 25 '13 at 13:54

2 Answers2

9

The int probably has a four-byte alignment requirement, so in the first case, both of the char elements need to have three padding bytes appended to them but in the second case you only need two padding bytes after the second char element (because a char element has an alignment of one byte).

word1 looks like:

0   |1   |2   |3   |4   |5   |6   |7   |8   |9   |10  |11
a   |  (padding)   |b                  |c   |  (padding)

word2 looks like:

0   |1   |2   |3   |4   |5   |6   |7
a   |b   |(padding)|c               
James McNellis
  • 348,265
  • 75
  • 913
  • 977
4

Case 1:

  0    1    2    3    4  
+---------------------+
| a    | Unused       |      4 bytes
+---------------------+

  0    1    2    3    4 
+---------------------+
|       b             |      4 bytes
+---------------------+

  0    1    2    3    4 
+---------------------+ 
| c    | Unused       |      4 bytes
+---------------------+

Total : 12

Case 2:

  0    1    2    3    4 
+---------------------+
| a   | b   | Unused  |      4 bytes
+---------------------+

  0    1    2    3    4 
+---------------------+
|       c             |      4 bytes
+---------------------+

Total : 8

P.S : Structure Padding is implementation defined.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345