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
-
#include<stdio.h>
-
-
#pragma pack(push)
-
-
#pragma pack(1)
-
-
struct test{
-
-
char a;//1 byte //Genrally padded so that int can start from 4 byte alignment
-
-
int b;//
-
-
char c;//1 byte //generally padded so that d can start in 2 byte allignment
-
-
short d;
-
-
};
-
-
#pragma pack(pop)
-
-
typedef struct test testStructure;
-
-
int main()
-
-
{
-
-
testStructure testData;
-
-
testStructure testDataArray[4];
-
-
int iterator;
-
-
printf("\n Address of test Data is 0x %x \n", &testData);
-
-
printf(" Address of char a in testData is 0x %x \n", &(testData.a));
-
-
printf(" Address of int b in testData is 0x %x \n", &(testData.b));
-
-
printf(" Address of char c in testData is 0x %x \n", &(testData.c));
-
-
printf(" Address of short d in testData is 0x %x \n", &(testData.d));
-
-
printf(" size of struct test is %d\n", sizeof(struct test));
-
-
printf("\n Printing the address in Array\n");
-
-
for(iterator = 0; iterator < 4; iterator ++)
-
-
{
-
-
printf("Iterator value is %d \n", iterator);
-
-
printf("\n Address of testDataArray is 0x %x \n", &testDataArray[iterator]);
-
-
printf(" Address of char a in testDataArray is 0x %x \n", &(testDataArray[iterator].a));
-
-
printf(" Address of int b in testDataArray is 0x %x \n", &(testDataArray[iterator].b));
-
-
printf(" Address of char c in testDataArray is 0x %x \n", &(testDataArray[iterator].c));
-
-
printf(" Address of short d in testDataArray is 0x %x \n", &(testDataArray[iterator].d));
-
-
}
-
-
return 0;
-
-
-
-
}
-
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.