sizeof behavior
Read the following lines and weep.. Why does sizeof return 4? :confused: :confused:
typedef struct tagS {
BYTE aaa[4];
DWORD bbb;
WORD ccc;
BYTE ddd[8];
WORD eee;
} S;
class CS : public S {
public:
CS() {}
};
sizeof(S) returns 20
sizeof(CS) returns 4
# 1 Re: sizeof behavior
That is correct. Lets break it down...
sizeof(S) = 20 because you have 12 bytes, a dword (double word = 8 bytes) and two words (4 bytes each) but they are not all initialized.
sizeof(CS) is returning the size of the pointer of CS which will always equal 4 bytes.
# 3 Re: sizeof behavior
typedef struct tagS {
BYTE aaa[4];
DWORD bbb;
WORD ccc;
BYTE ddd[8];
WORD eee;
} S;
class CS : public S {
public:
CS() {}
};
TRACE("S = %d, CS = %d\n", sizeof(S), sizeof(CS));
This prints "S = 20, CS = 20" as expected on my machine. S is the base object of CS. As long as CS doesn't have other data members and any virtual functions, I expect S and CS to have the same size. Note that structure packing can play a role in this.
# 4 Re: sizeof behavior
Originally posted by zebbedi
sizeof(S) = 20 because you have 12 bytes, a dword (double word = 8 bytes) and two words (4 bytes each) but they are not all initialized.
Sorry...but that is not true...'DWORD' is nothing else than a typedef for an 'unsigned int' which is 4 bytes, and 'WORD' is nothing else than a typedef for 'unsigned short' which in turn is 2 bytes.
The size of the structure is
typedef struct tagS
{
BYTE aaa[4]; // 4 bytes
DWORD bbb; // 4 bytes
WORD ccc; // 2 bytes
BYTE ddd[8]; // 8 bytes
WORD eee; // 2 bytes
} S;
# 5 Re: sizeof behavior
Originally posted by zebbedi
sizeof(CS) is returning the size of the pointer of CS which will always equal 4 bytes.
Where is pointer mentioned there?
# 7 Re: sizeof behavior
People, I stand ashamed and apologize for the mess I made.
It is just a stupid mistake of copying the wrong name.
Again, my apologies.:eek: