13,052,696 members (65,533 online)
Rate this:
See more:
Hi,all.
I am new to c++ and trying to figure out the amount of memory allocated to union to strut. Underneath is the code:
```struct structure1
{
int i;
double d;
char ch;
};

union Packed
{
int i;
double d;
char ch;
}

int main(void)
{
structure1 objstruct;
Packed objP;
std::cout<<sizeof(objstruct);
std::cout<<sizeof(objP);
}```

The output size of objstruct is not the arithmetic addition of its respective types, instead it is 24. Can someone help explain the reason?
For union, no matter how many items you have, the memory it occupies is always the size of the type which consume the maximum amount of memory. In this case, double.
How this is achieved?

Posted 4-Dec-11 15:38pm
Updated 4-Dec-11 15:39pm
v2
Eduard Lu 4-Dec-11 21:39pm

Rate this:

## Solution 1

Why 24 bytes? This is the size of 3 8-bytes (64-bit) words. You probably use 64-bit system. The size of int is system-dependent. And no, the structure size does not have to be the sum of the member sizes. It depends on structure memory alignment which can be different. Is is usual to use alignment by 32-bit or 64-bit boundary. For example, if a structure is 64-bit aligned, it will allocate 64*3 bits or 24 bytes if one member needs 64 bits and two other need 8 bits.

As to the second question, this is not a question. What do you mean by "How this is achieved?"? This is not "achieved", this is done by definition of the union.

—SA
v2
Rate this:

## Solution 4

It is called: Data structure padding.
If you change the order of structure members this way:
```struct structure1
{
int i;//4
double d;//8
};```

-you will get 16 instead of 24.

Rate this:

## Solution 2

```#pragma pack(push)   // Save setting
#pragma pack(1)      // 1 byte packing alignment for structure,
// union, and class members.
struct stTest
{
int i;
double d;
char ch;
};
#pragma pack(pop)    // Restore setting```
v3
Albert Holguin 7-Dec-11 9:19am

Although you shouldn't mess with the structure packing unless you know what you're doing and there's some reason to do so (such as interacting with hardware or socket programming).
Rate this:

## Solution 3

Thank you,my PC is 32 bit.
What i really meant is the to know the mechansim of memory alignment.Can anyone briefly explain here?
Albert Holguin 7-Dec-11 9:20am

Don't post follow up questions as solutions. If you mean to ask a follow-up to someone's solution, use the "Have a Question or Comment?" link, otherwise use the "Improve question" link.
JackDingler 7-Dec-11 10:18am

Processors read and write memory in discrete units. A 32 bit processor typically reads and writes in 32 bit chunks. when variables are aligned at 32 bit boundaries, then the processor doesn't need to move the bits around to find the data. It's already aligned to the boundary.

But say your data is two bytes from the boundary and consists of four bytes. The process has to read four bytes to get the first half of the data, then four more bytes to get the second half of the data. Then it has to merge the two together, to reconstruct the original four bytes. If that four bytes were already aligned on the boundary, all of this extra work would not have to be done.

This is why by default, the compiler aligns every data member on the 32bit or 64bit boundary. It makes your code run more efficiently.

Albert points out that there are legitimate reasons for changing the packing alignment. This is typically needed when storing and reading data from files and streams. This allows you to decide exactly where every byte gets put. It can be more efficient in memory in memory usage but you pay a penalty in processor time and energy consumption.
Member 8241963 8-Dec-11 1:55am

Thank you, that's what I want.
Stefan_Lang 9-Dec-11 4:31am

You keep responding to yourself. If you wanted to respond to JackDinglers comment, you should use the 'Reply' link on the top right of that comment.

Top Experts
Last 24hrsThis month
 OriginalGriff 295 Jochen Arndt 120 Richard MacCutchan 95 Afzaal Ahmad Zeeshan 80 Member 10192073 75
 OriginalGriff 5,581 RickZeeland 1,954 ppolymorphe 1,858 F-ES Sitecore 1,636 Richard MacCutchan 1,429

Web01 | 2.8.170713.1 | Last Updated 8 Dec 2011