Solved

sizeof() union

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
modThree challenge 4 96
Sed question 2 101
Not needed 13 119
Way to decrease size of apk file 9 86
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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