Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ size Data
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?
 
Thank you in advance.
Posted 4-Dec-11 16:38pm
Edited 4-Dec-11 16:39pm
v2
Comments
Eduard Lu at 4-Dec-11 21:39pm
   
EDIT: added "pre" tag
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This article explains this very nicely: http://msdn.microsoft.com/en-us/library/2e70t5y1(v=vs.80).aspx[^]
 
#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
  Permalink  
v3
Comments
Albert Holguin at 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: bad
good
Please Sign up or sign in to vote.

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?
  Permalink  
Comments
Albert Holguin at 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 at 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 at 8-Dec-11 1:55am
   
Thank you, that's what I want.
Stefan_Lang at 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)

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 8 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100