How to avoid structure padding in C?
Answer Posted / lokesh mogra
yes u can use pragma to change change byte alignment.
for e.g.
typedef struct _s1{
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
} s1;
Size of this structure is of 11 Bytes. but due to default
byte alignment(8 byte) which is different for different
compilers. The size of structure would be 16 Bytes.
In order to change the alignment, we will have to do
something like this.
#pragma pack(push,1)
typedef struct _s1{
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
//unsigned char b;
} s1;
#pragma pack(pop)
This will change the byte alignment to 1 Byte. and thus size
of structure will be exactly 11 bytes
Is This Answer Correct ? | 49 Yes | 11 No |
Post New Answer View All Answers
Explain what is the benefit of using #define to declare a constant?
Difference between exit() and _exit() function?
Explain how do you generate random numbers in c?
Why c is a procedural language?
Why c is called a mid level programming language?
Explain modulus operator.
What is c basic?
Why do we use header files in c?
What is the equivalent code of the following statement in WHILE LOOP format?
Difference between pass by reference and pass by value?
What is the function of this pointer?
Is that possible to add pointers to each other?
PLS U SENS ME INTERVIEW O. MY EMAIL ADD, SOFIYA.SINGH@GMAIL.COM
What are different types of operators?
Why do we need functions in c?