1

Considering an int takes 4 bytes and char takes 1 byte, the size occupied by below structure should be 9 [ 4 + 1 + 4 ], but actually it is 12 bytes [ 4 + 4 + 4 ].

Reason :
char y reserves 4 byte of memory, and y occupy 1 byte and rest 3 allocated bytes are not used.

struct some_struct { int x; char y; int z; }; 

question 1 : so why does compiler behaves so ? (suppose i have a X8086 architecture)

question 2 : what if it were a union (not struct) ?


Thanks very much all for answers , i realize i should have searched for it on SO only before posting question .

4
  • 4
    1. Because alignment. 2. What if you got yourself a good book? Commented Jan 8, 2014 at 12:22
  • 3
    This question comes up quite often. I have found one such duplicate. The answer is padding. Commented Jan 8, 2014 at 12:22
  • As to why date type padding occurs in a structure, it's due to efficiency of access by the code. See en.wikipedia.org/wiki/Data_structure_alignment for details. A union cannot be used instead since it does something different than a struct, unless you really want what a union does, which is to overlap all the members at the same mememoy location. Commented Jan 8, 2014 at 12:22
  • @ac_c0der You really mean 8086? AFAIR, 8086 is 16-bit so I guess, that padding (if any) would be to 2 bytes, not to 4... Commented Jan 8, 2014 at 12:33

3 Answers 3

2

The term you are looking for is padding. And it greatly depends on the architecture. Some architectures support "packed" structures.

No assumptions can be made about unless the compiler on given machine explicitely states what kind of padding is used. C/C++ standards do not define that.

Often the amount of padding will depend on the current and next field size. If first field is 1 byte large, and the second is 4 bytes, then the first field will be padded with 3 bytes. This is related to variable addressing issue - it sometimes is not possible to allocate a four-byte variable on a non %4==0 address. And even if it is possible - it may introduce an additional overhead and make the code work slower, so it is usually not done by default. #pragma pack can alter that behavior in VS.

Union is a completely different beast. It uses the same memory for all members. Its size is usually equal to the size of its largest members. Other members simply don't use all its memory.

Sign up to request clarification or add additional context in comments.

1 Comment

"it sometimes is not possible to allocate..." right - or it is more expensive to be unaligned (the softer version of it).
0

for your 1st question refer this [a link] ( http://www.avabodh.com/cin/structure.html ) and for 2nd question refer this a link .

Comments

0
  • For struct: its elements are allocated with the order as they declared in the struct. It places each of its members in separate memory in a single, contiguous area. See How are C struct members allocated? for more info.

  • For union, as the name suggests, defines a structure where all of its members occupy the same memory space. Check out Unions versus structures in C for examples.

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.