Click here to Skip to main content
13,351,985 members (78,059 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Here we have a structure:
struct FOO

Does the following expression return a reliable value in such cases?

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 5:19am
Rate this: bad
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:[^] - at "Structure Padding", near the bottom.
Joseph Marzbany 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.
Joseph Marzbany 27-Oct-11 10:47am
and what if I want to write a platform/compiler-independent code?
Albert Holguin 27-Oct-11 10:56am
Agree w/ OG, good questions... :)
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:
SAKryukov 27-Oct-11 14:30pm
Good explanation, but "#pragma pack" information is important here. A 5, anyway.
Joseph Marzbany 27-Oct-11 15:25pm
Artificial platform? What's that? Could you give a clue?
OriginalGriff 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 28-Oct-11 6:53am
awful :)
Rate this: bad
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.



gcc equivalent:[^]
Albert Holguin 27-Oct-11 10:55am
Great addition! +5... this is specially important when dealing with network communications or interacting with hardware...
Nishant Sivakumar 27-Oct-11 10:59am
Joseph Marzbany 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
Nishant Sivakumar 27-Oct-11 12:31pm
gcc supports it too. But do check the docs for compatibility:
SAKryukov 27-Oct-11 14:29pm
Good point, a 5.
Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 28 Oct 2011
Copyright © CodeProject, 1999-2018
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