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
void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }
What is #include stdlib h?
Describe dynamic data structure in c programming language?
What is the difference between scanf and fscanf?
What does struct node * mean?
What are identifiers c?
What is the ANSI C Standard?
Why do we need functions in c?
Write an algorithm for implementing insertion and deletion operations in a singly linked list using arrays ?
How would you rename a function in C?
Should I learn c before c++?
What is the difference between test design and test case design?
what is the significance of static storage class specifier?
largest Of three Number using without if condition?
Why main is used in c?