Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Here we have a structure:
struct FOO
{
    Variable1;
    Variable2;
    ...
}
Does the following expression return a reliable value in such cases?
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 27-Oct-11 4:19am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It does return a reliable value - just not always the value you expect. For example, if your structure contained:
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.
  Permalink  
Comments
Joseph Marzbany at 27-Oct-11 10:33am
   
Thank you again. Is this always the case? I mean Isn't it Platform-Dependent or something?
OriginalGriff at 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 at 27-Oct-11 10:56am
   
Agree w/ OG, good questions... :)
Joseph Marzbany at 27-Oct-11 10:47am
   
and what if I want to write a platform/compiler-independent code?
OriginalGriff at 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:
Joseph Marzbany at 27-Oct-11 15:25pm
   
Artificial platform? What's that? Could you give a clue?
OriginalGriff at 27-Oct-11 15:43pm
   
Think of an "ideal" processor and write for that. Then all you have to do is either convert your "ideal" into the real processor, or write an emulator that runs your "ideal" processor. Be aware it is a serious amount of work!
If you are interested, look into the background of .NET: it's fascinating stuff and basically covers what you are talking about (probably in more detail than you will ever need :laugh:)
Joseph Marzbany at 28-Oct-11 6:53am
   
awful :)
SAKryukov at 27-Oct-11 14:30pm
   
Good explanation, but "#pragma pack" information is important here. A 5, anyway.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
v3
Comments
Albert Holguin at 27-Oct-11 10:55am
   
Great addition! +5... this is specially important when dealing with network communications or interacting with hardware...
Nishant Sivakumar at 27-Oct-11 10:59am
   
Thanks.
Joseph Marzbany at 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 at 27-Oct-11 11:18am
   
pragma directives are not VC++ specific
Nishant Sivakumar at 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
SAKryukov at 27-Oct-11 14:29pm
   
Good point, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 28 Oct 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