could some one give me a detail of this the result??I don not know why,more detail more better
-
#include <iostream>
using namespace std;
#pragma pack(8)struct example1
{
short a;
long b;
};struct example2
{
char c;
example1 struct1;
short e;
};#pragma pack()
int main()
{
example2 struct2;
cout << sizeof(example1) << endl; //8
cout << sizeof(example2) << endl; //16
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl; //4getchar();
}
-
#include <iostream>
using namespace std;
#pragma pack(8)struct example1
{
short a;
long b;
};struct example2
{
char c;
example1 struct1;
short e;
};#pragma pack()
int main()
{
example2 struct2;
cout << sizeof(example1) << endl; //8
cout << sizeof(example2) << endl; //16
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl; //4getchar();
}
I am not sure what your problem is, but i will guess.
wbgxx wrote:
cout << sizeof(example1) << endl; //8
- here you wonder why you get 8 instead of 6 when short is suposed to be 2 bytes and long is suposed to be 4 bytes, so 2+4 is 6, not 8.
wbgxx wrote:
cout << sizeof(example2) << endl; //16
- here you wonder why you get 16 when char is 1 byte, short is 2 byte and your struct1 is 8 bytes so you expect it to be 11 bytes altogether, not 16.
wbgxx wrote:
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2) << endl; //4
- here you wonder why you get 4 bytes instead of 1 byte, since there's only a char infront of the struct2 member which is suposed to be 1 byte long, not 4. If i am right then see this[^] and this[^].
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
#include <iostream>
using namespace std;
#pragma pack(8)struct example1
{
short a;
long b;
};struct example2
{
char c;
example1 struct1;
short e;
};#pragma pack()
int main()
{
example2 struct2;
cout << sizeof(example1) << endl; //8
cout << sizeof(example2) << endl; //16
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl; //4getchar();
}
Code-o-mat has pointed in the right direction. It has everything to do with the structure packing and the controlling
#pragma pack
directive. In you're example it says#pragma pack(8)
. This means pack the struct on 8 byte boundaries. For the structureexample1
,short
is 2 bytes andlong
is 4 bytes. Here the compiler will reserve 4 bytes for theshort
so that it becomes 8 bytes total. If you change the pragma to#pragma pack(1)
, you should get the results as 6 and 9.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
#include <iostream>
using namespace std;
#pragma pack(8)struct example1
{
short a;
long b;
};struct example2
{
char c;
example1 struct1;
short e;
};#pragma pack()
int main()
{
example2 struct2;
cout << sizeof(example1) << endl; //8
cout << sizeof(example2) << endl; //16
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl; //4getchar();
}
-
cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl;and what is (unsigned int)(&struct2.struct1)means?
wbgxx wrote:
(unsigned int)(&struct2.struct1
You first take the address of a variable... Then cast it to a number, so when cout "prints" it, it will be shown as a number, not some fancier thing. As the answers to your (slightly) later question said, this is very basic C/C++ stuff - a book is better able to help you than a message board. Pointers are one of that concepts that can explained for ages, but you'll wake up one morning and go "Oooooh!". Think of the difference between the place you live - and address written on a postcard to you. Good luck with your journey. Iain.
I have now moved to Sweden for love (awwww).