473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

what is structure padding

hi,
can u pls tell me abt structure padding and the memory pools.how to find out memory leaks.
Oct 3 '06 #1
4 91549
Banfa
9,065 Expert Mod 8TB
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
tyreld
144 100+
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
svlsr2000
181 Expert 100+
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
9,065 Expert Mod 8TB
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

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

Similar topics

13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
5
by: Stephen Mayes | last post by:
I this helloworld portable? I am vaguely aware of something called "structure padding" and wonder if it could affect this program since the struct only contains chars. #include <stdio.h>...
1
by: pmm | last post by:
hi I am repeating my post here plz excuse i am trying out a UDP packet transfer between a windows machine and a linux I created a structure on both sides (ie on linux and on windows) and I sent...
3
by: abhivg | last post by:
Hi, I am trying to port a 32 bit Unix application to 64 bit Windows. While compiling on Windows I am getting a number of warnings related to structure padding. More specifically "warning C4820:...
2
by: arunraj2002in | last post by:
gcc -O2 -S -c b.c gives the answer but if we declare a structure say struct test_t { int a; char b; int c; }; main() { struct test_t test= { 13,30, 40}; }
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
10
by: Rohit kumar Chandel | last post by:
Hi All, Please let me know how to find in a structure whether compiler has used padding or not. Regards Rohit
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.