Solved

sizeof() union

Posted on 2004-04-13
3
402 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 58
Pressing cancel button with C# UI Automation 3 40
WinWaitActive parameters 12 31
Sed question 2 49
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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. …
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 video discusses moving either the default database or any database to a new volume.

762 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

23 Experts available now in Live!

Get 1:1 Help Now