Solved

sizeof() union

Posted on 2004-04-13
3
405 Views
Last Modified: 2013-11-20
Maybe I just counted the bytes wrong,  but the sizeof() function seems to return an incorrect value when passed certain unions in my program.

PROBLEM:  sizeof(UCell) returns 16, I believe it should be 12.


union UData
{
    char*      m_szData;
    double      m_dData;

    UData():m_dData(0){};
};

union UCell
{
    struct
    {
        UData  m_Data;
        BOOL         m_bDouble;
    };

    char m_pBuffer[12];
};

If I have counted the bytes wrong, please explain.  If there is a known problem with sizeof() I would like to know how to work around it, UCell is used in multiple classes and would be bad practive to have to hard code it's size for use when writing or reading files.
0
Comment
Question by:r2far
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10815929
>>If I have counted the bytes wrong, please explain.

You have, indeed. 'sizeof()' will always return the size of the largest member of the union *plus* the padding for the default alignment (4 bytes usually). So, you have 'UData' with 12 bytes ( char* is 4 bytes, double is 8 bytes) plus 4 bytes for the BOOL (actually, BOOL is a 'long' - wtypes.h uses 'typedef long BOOL;') which makes it exactly 16.
0
 
LVL 4

Author Comment

by:r2far
ID: 10816247
thanx for your response.  I do know the size of most defualt data types :)

>>So, you have 'UData' with 12 bytes ( char* is 4 bytes, double is 8 bytes)

UData is a union, should it not be 8 bytes long (size of it's largest member)?  I got 12 under the believe that UData would be 8 (the char* shares same mem space as the first 4 bytes of the double)
0
 
LVL 86

Accepted Solution

by:
jkr earned 75 total points
ID: 10816296
Then, the alignment might be the reason. Try to compile it with

#pragma pack(1)

The default packing size is 8 (according to the docs, I was wrong about 4), which would also explain 16 as the result.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now