I have been working on my friends post
here is my test case:
————————————————————————————————————————–
ert@SEMERKAND:~$ cat dnm.cpp
#include <iostream>
union manken_u {
char ch;
int integer;
double db;
};
#pragma pack(2)
struct manken_s_p {
char ch;
int integer;
double db;
};
#pragma pack()
struct manken_s {
char ch;
int integer;
double db;
};
#define sub(x,y) (int)(x)-(int)(y)
int main()
{
std::cout
<< ”size of union: ” << sizeof(union manken_u) << std::endl
<< ”size of struct: ” << sizeof(struct manken_s) << std::endl
<< ”size of packed struct: ” << sizeof(struct manken_s_p) << std::endl
<< ”size of char+int+double: ” << sizeof(char)+sizeof(int)+sizeof(double) << std::endl;
manken_s my_struct;
char* base = &my_struct.ch;
std::cout
<< ”offset of char ” << sub(base,&my_struct.ch) << std::endl
<< ”offset of int ” << sub(base,&my_struct.integer) << std::endl
<< ”offset of db ” << sub(base,&my_struct.db) << std::endl;
std::cout << ” pack is 2 ” << std::endl;
manken_s_p my_struct_p;
base = &my_struct_p.ch;
std::cout
<< ”offset of char ” << sub(base,&my_struct_p.ch) << std::endl
<< ”offset of int ” << sub(base,&my_struct_p.integer) << std::endl
<< ”offset of db ” << sub(base,&my_struct_p.db) << std::endl;
return 0;
}
ert@SEMERKAND:~$ g++ dnm.cpp
ert@SEMERKAND:~$ ./a.out
size of union: 8
size of struct: 16
size of packed struct: 14
size of char+int+double: 13
offset of char 0
offset of int -4
offset of db -8
pack is 2
offset of char 0
offset of int -2
offset of db -6
————————————————————————————————————————–
So, what is the moral of the story:
#pragma pack is a directive for compiler to determine the size of atomic unit for storing nonprimitive data types (struct for example) on memory.
If you set it to 2, chars in a struct will have a size of 2 bytes not 1 byte, because compiler will align it.
In my example first offset values are for pack(4) (default), so char has a padding value of 3 bytes to expand it to 4 bytes.
Following offset values shows pack(2) results. Char has a total lenght of 2 byte. BUT IT DOES NOT USE 2 BYTE.
And…. Why the compiler does this alignment stuff. Because different arc. have certain types of sizes of types like this
references:
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/compiler/ref/rnpgpack.htm
http://docs.hp.com/en/8/pragmas.htm
and http://emreknlk.blogspot.com/
One Comment
Thank yor Erturk, i suppose it is worth to mention here that, in the below code if you comment out the first commentOut variable the struct size doesn’t change! because 2 char can be stored in sequantially 2byte alignment. So the struct size is still 14byte.
But if you comment out the second variable the struct size will be 16 byte! Because the second char goes after the integer which is placed in a 2*N bytes unit. So in the first situation we add an extra element and we lose no memory! On the other hand 2nd situation causes to extra 2bytes for 1 extra variable. It is very important the order and number of variables in struct. It should be well designed
#include
#pragma pack(2)
struct manken_s_p {
char ch;
// char commentOut;
int integer;
// char commentOut;
double db;
};
#pragma pack()
int main()
{
std::cout