Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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:
C++
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?

Thank you in advance.
Posted
Updated 4-Dec-11 15:39pm
v2
Comments
[no name] 4-Dec-11 21:39pm    
EDIT: added "pre" tag

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
 
Share this answer
 
v2
It is called: Data structure padding.
If you change the order of structure members this way:
struct structure1
{
	int i;//4
	char ch;//1+3 (padding)
	double d;//8
};

-you will get 16 instead of 24.

Here structure padding is explained good enough: Structure padding in C
 
Share this answer
 
This article explains this very nicely: http://msdn.microsoft.com/en-us/library/2e70t5y1(v=vs.80).aspx[^]

C#
#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
 
Share this answer
 
v3
Comments
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).
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?
 
Share this answer
 
Comments
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900