3

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?

7
  • Don't use 'void main()' and expect to be unshouted at - the correct return type for main() is int. Commented Oct 24, 2010 at 7:25
  • one day someone is going to claim that returning void instead of int caused a nuclear meltdown, or something. Commented Oct 24, 2010 at 7:27
  • OK, I'll take care of this from next time. Commented Oct 24, 2010 at 7: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. Commented Oct 24, 2010 at 7:35
  • Gotta be one of the most duplicated on-topic question on Stack Overflow. Commented Oct 24, 2010 at 19:33

2 Answers 2

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               
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.