Why padding in structure




















So, 32 bit processor always reads 4 bytes at a time and 64 bit processor always reads 8 bytes at a time. This concept is very useful to increase the processor speed. To make use of this advantage, memory is arranged as a group of 4 bytes in 32 bit processor and 8 bytes in 64 bit processor. Below C program is compiled and executed in 32 bit compiler.

Please check memory allocated for structure1 and structure2 in below program. In above program, memory for structure1 is allocated sequentially for first 4 members. Range of this 4 byte package is from to Addresses and only is available in this package. Due to the change in the order, the output would be different in both the cases.

In the previous case, the output was 8 bytes, but in this case, the output is 12 bytes, as we can observe in the below screenshot. The structural padding is an in-built process that is automatically done by the compiler. Sometimes it required to avoid the structure padding in C as it makes the size of the structure greater than the size of the structure members.

In the above code, we have used the pragma pack 1 directive to avoid the structure padding. If we do not use this directive, then the output of the above program would be 16 bytes.

But the actual size of the structure members is 13 bytes, so 3 bytes are wasted. To avoid the wastage of memory, we use the pragma pack 1 directive to provide the 1-byte packaging. JavaTpoint offers too many high quality services.

Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week. Command Line Arguments. Next Topic C Union. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow.

Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. You can also go through our other related articles to learn more —.

Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? Asked 6 years, 5 months ago. Active 6 years, 5 months ago. Viewed 2k times. Community Bot 1 1 1 silver badge. Subinoy Subinoy 5 5 silver badges 22 22 bronze badges. Read: Alignment — P. This solved my issue: stackoverflow.

Sorry for duplicate question but I searched a lot but couldn't find the answer properly — Subinoy. To answer your question in a word its performance. Add a comment.



0コメント

  • 1000 / 1000