Jump to content
  • Advertisement
Sign in to follow this  
xIshtarx

2+4=8

This topic is 5473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can some1 explain me this? if i check this size of this structure: struct Structure { short data; int data2; }; its 8 instead of 6 ( sizeof(short)+sizeof(int) ) xIshtarx

Share this post


Link to post
Share on other sites
Advertisement
The compiler will often pad structures up to 4-byte alignment.

If you call #pragma pack(1) at the start of your file, you'll get 1-byte alignment.

Have a look at MSDN for more info.

Share this post


Link to post
Share on other sites
Quote:
Original post by xIshtarx
Can some1 explain me this?
if i check this size of this structure:

struct Structure
{
short data;
int data2;
};

its 8 instead of 6 ( sizeof(short)+sizeof(int) )

xIshtarx


It's called padding, and the Compiler does it so that the integer will be aligned to a specific memory boundry. In your case, it's a 4 byte boundry on the int. So you have two extra bytes between data and data2.

Quote:

In a 32bit OS, short int and long are all 4 bytes. So is a float. Char is 1 byte, double is 8 bytes, long double is also 8 bytes.

Not true in the slightest.

Share this post


Link to post
Share on other sites
Quote:
Original post by evolutional
The compiler will often pad structures up to 4-byte alignment.

If you call #pragma pack(1) at the start of your code, you'll get 1-byte alignment.

Have a look at MSDN for more info.


You really shouldn't do that though. As it will have performance costs, and tends to make your code anything but portable. If you're saving and loading it to/from a file and the padding is getting in the way (say TGA loading), then the best bet is to work with the individual members of the structure.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Quote:

In a 32bit OS, short int and long are all 4 bytes. So is a float. Char is 1 byte, double is 8 bytes, long double is also 8 bytes.

Not true in the slightest.



char: 1 byte (not sure if this is always true)
short: 2 bytes
int: 4 bytes
long: 4 bytes
long long: 8 bytes
float: 4 bytes
double: 8 bytes
long double: 10 bytes (not sure if this is always true)


Thermo

Share this post


Link to post
Share on other sites
Most compilers try to align structure members on boundry that produces better access code. If you are using VC6, you can change this behavior under the C/C++ code generation setting tab. The default is 8 bytes.

You can also use the compile directive:

#praga pack(1)

To force the compiler to pack a certain way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
Quote:
Original post by Washu
Quote:

In a 32bit OS, short int and long are all 4 bytes. So is a float. Char is 1 byte, double is 8 bytes, long double is also 8 bytes.

Not true in the slightest.



char: 1 byte (not sure if this is always true)
short: 2 bytes
int: 4 bytes
long: 4 bytes
long long: 8 bytes
float: 4 bytes
double: 8 bytes
long double: 10 bytes (not sure if this is always true)

Thermo

The only one there that is absolutely true is the char.
The rest are compiler dependant, although the standard does state that a float will be an IEEE single, so that should be 4 bytes, but it depends on the definition of a byte on the particular system.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
You really shouldn't do that though. As it will have performance costs, and tends to make your code anything but portable. If you're saving and loading it to/from a file and the padding is getting in the way (say TGA loading), then the best bet is to work with the individual members of the structure.


The one place I've found this useful is on my network packets, as having the extra unused overhead can add up real quick....

Otherwise, I agree, I wouldn't mess with it...

Share this post


Link to post
Share on other sites
Thanks for the replies guys :D
I was trieng to load something from a file, so ill just load each member individually.

xIshtarx

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!