Link to home
Start Free TrialLog in
Avatar of chep
chep

asked on

Visual C++ invalid sizeof or not ???

Is this bug or not ????

DWORD test
struct stest
{
DWORD v1;
WORD v2;
}

test = sizeof(stest)

Variable test must be equial to 6 but it returns 8
????????????????
Waiting for Answer :)


ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, see also the chapter 'sizeof' on the VC++ online docs:

sizeof Operator
sizeof expression

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).

When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.


Example

// Example of the sizeof keyword
size_t  i = sizeof( int );

struct align_depends {
    char c;
    int i;
};
size_t size = sizeof(align_depends);  // The value of size depends on
                                   //  the value set with /Zp or
                                   //  #pragma pack

int  array[] = { 1, 2, 3, 4, 5 };     // sizeof( array ) is 20
                                      // sizeof( array[0] ) is 4
size_t  sizearr =                        // Count of items in array
   sizeof( array ) / sizeof( array[0] );
Avatar of thienpnguyen
thienpnguyen

It is not a bug. It uses algnment structure for fast access.
Thnx - but why a 'B'? Is there anything I left open?