2+4=8

Started by
16 comments, last by Empirical 19 years, 7 months ago
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
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.
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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 bytesint:         4 byteslong:        4 byteslong long:   8 bytesfloat:       4 bytesdouble:      8 byteslong double: 10 bytes (not sure if this is always true)


Thermo
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.
--Robert Costellowwww.playfulminds.com
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 bytesint:         4 byteslong:        4 byteslong long:   8 bytesfloat:       4 bytesdouble:      8 byteslong 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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

And thats one C++ byte, which doesn't mean 8 bits.
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...
--Robert Costellowwww.playfulminds.com
Thanks for the replies guys :D
I was trieng to load something from a file, so ill just load each member individually.

xIshtarx

This topic is closed to new replies.

Advertisement