?
Solved

sizeof() union

Posted on 2004-04-13
3
Medium Priority
?
419 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 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