Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
com/topic/c/answers/543879-what-structure-padding
Development
Join Date: Oct 2006
123lakshmi Posts: 1
#1: Oct 3 '06
hi,
can u pls tell me abt structure padding and the memory pools.how to find out memory leaks.
Ads by Google
Space Frame Structures
www.nedtruss.com Glulam or steel Any size, any shape
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;
}
1 of 7 7/23/2010 6:01 AM
what is structure padding - C / C++ answers http://bytes.com/topic/c/answers/543879-what-structure-padding
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
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
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
s1 byte 1
s1 byte 2
c1
c2
2 of 7 7/23/2010 6:01 AM
what is structure padding - C / C++ answers http://bytes.com/topic/c/answers/543879-what-structure-padding
c3
padding byte
padding byte
padding byte
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.
hi,
can u pls tell me abt structure padding and the memory pools.how to find out memory leaks.
1.) The simple answer is compilers pad structures to optimize data transfers. This is an hardware
architecture issue. Most modern CPUs perform best when fundemental types, like 'int' or 'float', are algined
on memory boundarys of a particular size (eg. often a 4byte word on 32bit archs). Many architectures don't
allow misaligned access or if they do inccur a performance penalty.
When a compiler processes a structure declaration it will add extra bytes between fields to meet alignment
needs.
3 of 7 7/23/2010 6:01 AM
what is structure padding - C / C++ answers http://bytes.com/topic/c/answers/543879-what-structure-padding
1. struct MyStructA {
2. char a;
3. char b;
4. int c;
5. };
6.
7. struct MyStructB {
8. char a;
9. int c;
10. char b;
11. };
12.
13. int main(void) {
14. int sizeA = sizeof(struct MyStructA);
15. int sizeB = sizeof(struct MyStructB);
16.
17. printf("A = %d\n", sizeA);
18. printf("B = %d\n", sizeB);
19.
20. return 0;
21. }
22.
Results compiling with gcc on a Linux x86 system, it will vary for other CPU/OS/compiler combination. In
this example the gcc compiler is aligning the structure fields on 4byte boundaries. In MyStructA we have (1
byte + 1 byte + 2 bytes of padding + 4 bytes = 8bytes). In MyStructB we have (1 byte + 3 bytes of padding
+ 4 bytes + 1 byte + 3 bytes of padding = 12 bytes).
This example illustrates a good rule of thumb. Generally, it is good to group structure fields of the same
type together to minimize the extra padding.
3.) My laziness as a programmer is clearly showing as I'm going to refer you now to the wikipedia page
discussing memory leaks.
Structure Padding
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.
4 of 7 7/23/2010 6:01 AM
what is structure padding - C / C++ answers http://bytes.com/topic/c/answers/543879-what-structure-padding
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
I was just brushing up my c skills and especially structure padding. i have the following doubt
The output of this program compiled in windows xp, visual studio 2005 is as follows
5 of 7 7/23/2010 6:01 AM
what is structure padding - C / C++ answers http://bytes.com/topic/c/answers/543879-what-structure-padding
My doubt:
When we use #pragma pack the compilers usually stops padding the structure, then how are these variables
accessed. And this is contradictory with " 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".
Though we save much space here why the compilers dont have packing as default?
Though we save much space here why the compilers dont have packing as default?
The reason that compilers put in padding is to align members on boundaries that make the data easy to
access.
the int would be aligned on a 4 byte boundary because this allows the compiler to produce machine code
6 of 7 7/23/2010 6:01 AM
what is structure padding - C / C++ answers http://bytes.com/topic/c/answers/543879-what-structure-padding
using the processors 32bit access instructions (which often have to act on a 32bit boundary). By packing the
code into memory the machine code produced can no longer use the 32bit access routines and has to read
and load each byte of the int individually and reconstitute the 32 bit value in it's registers from the
individual bytes.
So the answer to your question is that while not padding the structure saves space it also makes the code
much more efficient and if memory is not at a premium then the wastage caused by padding is considered
worth it for the increased processor performance.
Ads by Google
Polymers
www.lanxess.in Polymers Manufacturer & Supplier For Orders, Contact LANXESS India
Tags
Similar topics
Size of a structure : Structure Padding ( C / C++ answers )
How can I peek at the assembly code generated by GCC -- Structure padding. ( C / C++ answers )
7 of 7 7/23/2010 6:01 AM