Solved

sizeof() union

Posted on 2004-04-13
3
414 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
[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
  • 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 Query 2 98
sameEnds challenge 3 187
move a line in eclipse 3 109
wordcount challenge 11 146
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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