Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual C++ invalid sizeof or not ???

Posted on 2002-05-07
4
Medium Priority
?
279 Views
Last Modified: 2013-12-14
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 :)


0
Comment
Question by:chep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 6994587
8 is correct, this is because of 'structure member alignment' - the compiler will add two padding bytes, so pointers to the strct are aligned on a DWORD boundary. To turn off that behaviour, use

#pragma pack ( 1)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6994595
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] );
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6994598
It is not a bug. It uses algnment structure for fast access.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6994720
Thnx - but why a 'B'? Is there anything I left open?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question