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

x
?
Solved

strange sizeof() behaviour!

Posted on 1999-07-16
7
Medium Priority
?
232 Views
Last Modified: 2010-04-02
Can someone produce this ?

typedef struct stSOMETHING
{
    unsigned long  l;
    unsigned short s;

} SOMETHING, *PSOMETHING;

int nSize = sizeof(SOMETHING);
// nSize = 8 ??!!

I'm on VC5.0 SP 3
NT4.0 SP 4
0
Comment
Question by:nil_dib
[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
  • 5
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1200386
that is not surprising.  the default allignment for VC is to a doubleword (4 byte) boundary.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200387
By default when the compiler defines a structure, it alligns the multi-byte values so that their offset within the structure is divisible by 4.  This way when objects of the structure are created, their members will start on doubleword boundaries (addresses that are evenly divisible by 4).  (The assumes that the structure is positioned so it starts on a doubleword boundary, which is something that the compiler will also do at every opportunity.)  This allows the data in the members to be accessed more quickly.
0
 
LVL 4

Author Comment

by:nil_dib
ID: 1200388
but:

typedef struct stSOMETHING
{
    unsigned short s;
} SOMETHING, *PSOMETHING;

int nSize = sizeof(SOMETHING);
// nSize = 2 !
 

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:nil_dib
ID: 1200389
>> the default allignment for VC
this is VC specific ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200390
You can control this process using the "pack" #pragma.  For example, you could do

#pragma pack (push,1)
typedef struct stSOMETHING
{
   unsigned long  l;
   unsigned short s;
} SOMETHING, *PSOMETHING;
#pragma pack(pop)

this makes the packing align to 1 but boundaries (no allignhment) while the structure is declared, then returns the alignment to it previous value.

Not also that you don't have to define structures using a typedef like that.  C++ considers structures to be complete types.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200391
>> this is VC specific ?
yes and no.

The C++ standard allows a compilers to do this.  Some do and some don't.  This depends on a) how concerned they are for speed verses size (it makes structures larger, but faster to use) and b) if the hardware the compiler is designed for can access aligned data faster than unaligned data.  (This is true of most modern CPUs, but older, simpler, CPUs didn't access aligned data any faster, so it would be a waste for them.)  Thus some compilers do so and others don't.  VC allows you to control it with the pragma, however, most other compilers won't do this, or may do so using a different syntax or approach (Like command-line options.)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200392
Note 80286 computers and later (like pentiums) all access data faster when alligned to doubleword boundaries, and even faster when alligned to paragraph (16 byte) boundaries). In addition all the win32 windows structures are defined assuming doubleword alignment, so any win32 compiler you find will do this.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

660 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