By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

what is structure padding

P: 1
hi,
can u pls tell me abt structure padding and the memory pools.how to find out memory leaks.
Oct 3 '06 #1
Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Oct 3 '06 #2

100+
P: 144
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
  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.
Oct 3 '06 #3

Expert 100+
P: 181
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.

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.  
  27. testStructure testData;
  28.  
  29. testStructure testDataArray[4];
  30.  
  31. int iterator;
  32.  
  33. printf("\n Address of test Data is 0x %x \n", &testData);
  34.  
  35. printf(" Address of char a in testData is 0x %x \n", &(testData.a));
  36.  
  37. printf(" Address of int b in testData is 0x %x \n", &(testData.b));
  38.  
  39. printf(" Address of char c in testData is 0x %x \n", &(testData.c));
  40.  
  41. printf(" Address of short d in testData is 0x %x \n", &(testData.d));
  42.  
  43. printf(" size of struct test is %d\n", sizeof(struct test));
  44.  
  45. printf("\n Printing the address in Array\n");
  46.  
  47. for(iterator = 0; iterator < 4; iterator ++)
  48.  
  49. {
  50.  
  51. printf("Iterator value is %d \n", iterator);
  52.  
  53. printf("\n Address of testDataArray is 0x %x \n", &testDataArray[iterator]);
  54.  
  55. printf(" Address of char a in testDataArray is 0x %x \n", &(testDataArray[iterator].a));
  56.  
  57. printf(" Address of int b in testDataArray is 0x %x \n", &(testDataArray[iterator].b));
  58.  
  59. printf(" Address of char c in testDataArray is 0x %x \n", &(testDataArray[iterator].c));
  60.  
  61. printf(" Address of short d in testDataArray is 0x %x \n", &(testDataArray[iterator].d));
  62.  
  63. }
  64.  
  65. return 0;
  66.  
  67.  
  68.  
  69. }
  70.  
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
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.
May 11 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
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 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.
May 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.