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
[324 byte] By [Major Major] at [2007-11-18 1:35:57]
# 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.
zebbedi at 2007-11-10 8:53:58 >
# 2 Re: sizeof behavior
sizeof(CS) is returning the size of the pointer of CS which will always equal 4 bytes.

What pointer? This is inheritence from a struct? I am confused..
Major Major at 2007-11-10 8:54:59 >
# 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.
Gabriel Fleseriu at 2007-11-10 8:55:56 >
# 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;
Andreas Masur at 2007-11-10 8:57:01 >
# 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?
pppsandeep at 2007-11-10 8:57:59 >
# 6 Re: sizeof behavior
I am sure you are not passing CS to sizeof. Might be you r passing CS * cs , sizeof(cs) ; like this.
Krishnaa at 2007-11-10 8:58:58 >
# 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:
Major Major at 2007-11-10 9:00:00 >