Discussion
How many bytes does the structure below occupy?
#include <stdio.h>
struct test
{
int k;
char c;
};
Answer: A
Correct answer: Multiple of integer size
The members are an int and a char, but the compiler adds padding so that the whole structure aligns to its strictest member. The size therefore comes out a multiple of the int size - 8 bytes on a typical machine, not the 5 the members occupy. Padding is what makes sizeof a structure unpredictable from its members alone.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development