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
any restrictions have on the number of 'return' statements that may be present in a function. a) no restriction b) only 2 return statements c) only 1 return statements d) none of the above
Write programs for String Reversal & Palindrome check
How many bytes are occupied by near, far and huge pointers (dos)?
What is the process to generate random numbers in c programming language?
What is New modifiers?
‘ C’ PROGRAME TO SHOW THE TYPE OF TRANGLE BY ACCEPTING IT’S LENGTH .
What are the standard predefined macros?
Is fortran still used in 2018?
Why do we need a structure?
a value that does not change during program execution a) variabe b) argument c) parameter d) none
What are loops c?
Can we access array using pointer in c language?
What are linked lists in c?
Dont ansi function prototypes render lint obsolete?
Draw a diagram showing how the operating system relates to users, application programs, and the computer hardware ?