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
Can we use any name in place of argv and argc as command line arguments?
What is the difference between the local variable and global variable in c?
writ a program to compare using strcmp VIVA and viva with its output.
What are reserved words?
Explain how can you check to see whether a symbol is defined?
What is static and volatile in c?
What is the function of multilevel pointer in c?
a function gets called when the function name is followed by a a) semicolon (;) b) period(.) c) ! d) none of the above
what do you mean by inline function in C?
What are the application of c?
Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above
How can I manipulate strings of multibyte characters?
What is the scope of static variable in c?
Is c procedural or functional?
explain what are pointers?