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


Please Help Members By Posting Answers For Below Questions

void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }

1476


What is #include stdlib h?

849


Describe dynamic data structure in c programming language?

809


What is the difference between scanf and fscanf?

870


What does struct node * mean?

796


What are identifiers c?

829


What is the ANSI C Standard?

996


Why do we need functions in c?

739


Write an algorithm for implementing insertion and deletion operations in a singly linked list using arrays ?

3256


How would you rename a function in C?

805


Should I learn c before c++?

961


What is the difference between test design and test case design?

1810


what is the significance of static storage class specifier?

1910


largest Of three Number using without if condition?

1254


Why main is used in c?

816