Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Here we have a structure:
C++
struct FOO
{
    Variable1;
    Variable2;
    ...
}

Does the following expression return a reliable value in such cases?
C++
sizeof(FOO)

I've heard the return value of sizeof on structures could be incorrect. If this is the case, what should I do to get the right value?
Posted

It does return a reliable value - just not always the value you expect. For example, if your structure contained:
C++
struct FOO
   {
   char ch;
   int i;
   }
And assuming a 1 byte char and 4 byte int, you do not get a result of "5" - the structure is padded so that elements start on their natural boundaries - you are more likely to get a result of "8".

Wiki explains this pretty well: http://en.wikipedia.org/wiki/Sizeof[^] - at "Structure Padding", near the bottom.
 
Share this answer
 
Comments
Joseph Marzbani 27-Oct-11 10:33am    
Thank you again. Is this always the case? I mean Isn't it Platform-Dependent or something?
OriginalGriff 27-Oct-11 10:37am    
No problem!
Yes it is platform dependant - good catch, gets you a 5 for the question! - it depends on the compiler that produces the code. Some processors can only work with ints (for example) that are aligned on word or long word boundaries, so any struct that contains them is likely to be padded to multiple of the boundary size.
That's why the result is "reliable" but may not be "expected" - the size of the structure is platform and compiler dependant.
Albert Holguin 27-Oct-11 10:56am    
Agree w/ OG, good questions... :)
Joseph Marzbani 27-Oct-11 10:47am    
and what if I want to write a platform/compiler-independent code?
OriginalGriff 27-Oct-11 14:14pm    
Then you have to create an "artificial platform" and provide a mechanism that runs the artificial platform on the real target platform and OS. Mind you that's not a bad idea, and it has been done before - that's pretty much what the .NET framework is! :laugh:
Adding to Griff's excellent response.

If you want manual control over struct alignment, and you are using VC++ then you can use #pragma pack.

See http://msdn.microsoft.com/en-us/library/2e70t5y1(v=VS.100).aspx[^]

[Update]
----------

gcc equivalent: http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html[^]
 
Share this answer
 
v3
Comments
Albert Holguin 27-Oct-11 10:55am    
Great addition! +5... this is specially important when dealing with network communications or interacting with hardware...
Nish Nishant 27-Oct-11 10:59am    
Thanks.
Joseph Marzbani 27-Oct-11 11:00am    
But I'd like to have a platform/compiler-independent code. I'm not going to use VC++ for ever
Albert Holguin 27-Oct-11 11:18am    
pragma directives are not VC++ specific
Nish Nishant 27-Oct-11 12:31pm    
gcc supports it too. But do check the docs for compatibility:
http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html
Well, generally the size does not matters that much between platforms. And if the data need to be serialized, then you should typically convert the data to a neutral representation explicitly so that you would properly handle things like the endianess, size and alignment.

Better yet, same your data in XML if in make sense for your application. It might be a bit slower but in many case, it could be benefical to have human readable data anyway.
 
Share this answer
 

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