How to avoid structure padding in C?
Answers were Sorted based on User's Feedback
Those are 3 different things.
Structure Padding
Most processors require specific memory alignment on
variables certain types. Normally the minimum alignment is
the size of the basic type in question, fo instance this is
common
char variables can be byte aligned and appear at any byte
boundary
short (2 byte) variables must be 2 byte aligned, they can
appear at any even byte boundary. This means that 0x10004567
is not a valid location for a short variable but 0x10004566 is.
long (4 byte) variables must be 4 byte aligned, they can
only appear at byte boundarys that are a multiple of 4
bytes. This means that 0x10004566 is not a valid location
for a long variable but 0x10004568 is.
Structure padding occurs because the members of the
structure must appear at the correect byte boundary, to
achieve this the compiler puts in padding bytes (or bits if
bit fields are in use) so that the structure members appear
in the correct location. Additionally the size of the
structure must be such that in an array of the structures
all the structures are correctly aligned in memory so there
may be padding bytes at the end of the structure too
struct example {
char c1;
short s1;
char c2;
long l1;
char c3;
}
In this structure, assuming the alignment scheme I have
previously stated then
c1 can appear at any byte boundary, however s1 must appear
at a 2 byte boundary so there is a padding byte between c1
and s1.
c2 can then appear in the available memory location, however
l1 must be at a 4 byte boundary so there are 3 padding bytes
between c2 and l1
c3 then appear in the available memory location, however
because the structure contains a long member the structure
must be 4 byte aligned and must be a multiple of 4 bytes in
size. Therefore there are 3 padding bytes at the end of the
structure. it would appear in memory in this order
c1
padding byte
s1 byte 1
s1 byte 2
c2
padding byte
padding byte
padding byte
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
c3
padding byte
padding byte
padding byte
The structure would be 16 bytes long.
re-written like this
struct example {
long l1;
short s1;
char c1;
char c2;
char c3;
}
Then l1 appears at the correct byte alignment, s1 will be
correctly aligned so no need for padding between l1 and s1.
c1, c2, c3 can appear at any location. The structure must be
a multiple of 4 bytes in size since it contains a long so 3
padding bytes appear after c3
It appears in memory in the order
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
s1 byte 1
s1 byte 2
c1
c2
c3
padding byte
padding byte
padding byte
and is only 12 bytes long.
I should point out that structure packing is platform and
compiler (and in some cases compiler switch) dependent.
Memory Pools are just a section of memory reserved for
allocating temporarily to other parts of the application
A memory leak occurs when you allocate some memory from the
heap(or a pool) and then delete all references to that
memory without returning it to the pool it was allocated from.
Is This Answer Correct ? | 59 Yes | 2 No |
Answer / 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 |
Answer / t swain
by using #pragma you can avoid structure padding.
Is This Answer Correct ? | 18 Yes | 2 No |
Answer / lakshman naganoor
By #pragma
Example:
#pragma pack(push,1)
struct mystruct_A {
int b;
short d;
double c;
char m;
};
#pragma pack(pop)
main()
{
printf("size of structure mystruct_Ad is %d\n",sizeof
(struct mystruct_A));
}
size of structure mystruct_Ad is 15
Note:size of structure mystruct_Ad without using #pragma
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / akshay singh
i think for avoid structure padding you use only the #pragma pack(1) its sufficient for structure padding
i am not sure about windows application this concept i used in linux or unix
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / santosh
Packing, on the other hand prevents compiler from doing padding - this has to be explicitly requested - under GCC it's __attribute__((__packed__)), so the following:
struct __attribute__((__packed__)) mystruct_A {
char a;
int b;
char c;
};
Is This Answer Correct ? | 0 Yes | 1 No |
18)struct base {int a,b; base(); int virtual function1(); } struct derv1:base{ int b,c,d; derv1() int virtual function1(); } struct derv2 : base {int a,e; } base::base() { a=2;b=3; } derv1::derv1(){ b=5; c=10;d=11;} base::function1() {return(100); } derv1::function1() { return(200); } main() base ba; derv1 d1,d2; printf("%d %d",d1.a,d1.b) o/p is a)a=2;b=3; b)a=3; b=2; c)a=5; b=10; d)none 19) for the above program answer the following q's main() base da; derv1 d1; derv2 d2; printf("%d %d %d",da.function1(),d1.function1(),d2.function1 ()); o/p is a)100,200,200; b)200,100,200; c)200,200,100; d)none 20)struct { int x; int y; }abc; you can not access x by the following 1)abc-->x; 2)abc[0]-->x; abc.x; (abc)-->x; a)1,2,3 b)2&3 c)1&2 d)1,3,4
write a c program to print "Welcome" without using semicolon in the whole program ??
Print all numbers which has a certain digit in a certain position eg: number=45687 1 number=4 2 number=5 etc
write a “Hello World” program in “c” without using a semicolon?
Dear Sir, we are required the bubble sorting programs Regs Prem
#include<stdio.h> #include<conio.h> void main() { char ch='\356'; printf("%d",ch); } o/p=-18 why?plz.explain
what different between c and c++
praagnovation
In a byte, what is the maximum decimal number that you can accommodate?
What are the ways to a null pointer can use in c programming language?
How can I read in an object file and jump to locations in it?
how to print this sereis 2 4 3 6 5..........?