Sei sulla pagina 1di 7

what is structure padding - C / C++ answers http://bytes.

com/topic/c/answers/543879-what-structure-padding

all Business IT Development sign up login

Ask your Question Write an Article Our Experts

bytes > c / c++ > c / c++ questions

what is structure padding


More channels
Ads by Google
Programming Business
www.drdobbs.com/Go-Parallel Stay on top of trends in parallel computing. Read our expert blog!
IT

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

Join Date: Feb 2006


Banfa Location: South West UK
E M C Posts: 7,142
#2: Oct 3 '06

re: what is structure padding

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;
}

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

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

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

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.

tyreld Join Date: Sep 2006


Posts: 144
C #3: Oct 3 '06

re: what is structure padding

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.

Expand | Select | Wrap | Line Numbers

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).

Expand | Select | Wrap | Line Numbers


1. tyreld@susie:~/Desktop> ./a.out
2. A = 8
3. B = 12
4.

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.

2.) The following link gives a basic overview of memory pools .

3.) My laziness as a programmer is clearly showing as I'm going to refer you now to the wikipedia page
discussing memory leaks.

Join Date: Feb 2007


svlsr2000 Location: Bangalore
E C Posts: 182
#4: May 11 '07

re: what is structure padding

Those are 3 different things.

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

Expand | Select | Wrap | Line Numbers


1. #include<stdio.h>
2.
3. #pragma pack(push)
4.
5. #pragma pack(1)
6.
7. struct test{
8.
9. char a;//1 byte //Genrally padded so that int can start from 4 byte alignment
10.
11. int b;//
12.
13. char c;//1 byte //generally padded so that d can start in 2 byte allignment
14.
15. short d;
16.
17. };
18.
19. #pragma pack(pop)
20.
21. typedef struct test testStructure;
22.
23. int main()
24.
25. {
26.

The output of this program compiled in windows xp, visual studio 2005 is as follows

Address of test Data is 0x 12ff50


Address of char a in testData is 0x 12ff50
Address of int b in testData is 0x 12ff51
Address of char c in testData is 0x 12ff55
Address of short d in testData is 0x 12ff56
size of struct test is 8
Printing the address in Array
Iterator value is 0
Address of testDataArray is 0x 12ff58
Address of char a in testDataArray is 0x 12ff58
Address of int b in testDataArray is 0x 12ff59
Address of char c in testDataArray is 0x 12ff5d
Address of short d in testDataArray is 0x 12ff5e
Iterator value is 1
Address of testDataArray is 0x 12ff60
Address of char a in testDataArray is 0x 12ff60

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

Address of int b in testDataArray is 0x 12ff61


Address of char c in testDataArray is 0x 12ff65
Address of short d in testDataArray is 0x 12ff66
Iterator value is 2
Address of testDataArray is 0x 12ff68
Address of char a in testDataArray is 0x 12ff68
Address of int b in testDataArray is 0x 12ff69
Address of char c in testDataArray is 0x 12ff6d
Address of short d in testDataArray is 0x 12ff6e
Iterator value is 3
Address of testDataArray is 0x 12ff70
Address of char a in testDataArray is 0x 12ff70
Address of int b in testDataArray is 0x 12ff71
Address of char c in testDataArray is 0x 12ff75
Address of short d in testDataArray is 0x 12ff76

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?

sorry for poor english.

Join Date: Feb 2006


Banfa Location: South West UK
E M C Posts: 7,142
#5: May 11 '07

re: what is structure padding

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.

Taking your example

Expand | Select | Wrap | Line Numbers


1. #pragma pack(1)
2.
3. struct test{
4.
5. char a;//1 byte //Genrally padded so that int can start from 4 byte alignment
6.
7. int b;//
8.
9. char c;//1 byte //generally padded so that d can start in 2 byte allignment
10.
11. short d;
12. };
13.

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.

This takes many more processor instructions.

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

c padding, padding c++, structure padding

« previous question | next question »

Similar topics
Size of a structure : Structure Padding ( C / C++ answers )

Structure padding ( C / C++ answers )

structure padding not considered by 'new' ( C / C++ answers )

How can I peek at the assembly code generated by GCC -- Structure padding. ( C / C++ answers )

structure layout question ( C / C++ answers )

querry related to structure padding ( C / C++ answers )

structure padding ( C / C++ answers )

What is the problem with my arp structure? ( C / C++ answers )

What type is a structure? ( C / C++ answers )

Structure padding. ( C / C++ answers )

Copyright 1995-2010 BYTES. All rights Reserved About Bytes | Help

Latest Expert Topics | Popular Tags


Sitemap | C / C++ Answers Sitemap | C / C++ Insights Sitemap

7 of 7 7/23/2010 6:01 AM

Potrebbero piacerti anche