views:

758

answers:

3

Visual C++ let's you select the struct members alignemnt in the project's properties page. Problem is, this configuration is being used for all srtructs in the project.

Is there any way (VC++ specific, I'd guess) to set a certain struct's member alignment individually?

+2  A: 
#pragma pack

http://msdn.microsoft.com/en-us/library/2e70t5y1(VS.80).aspx

KIV
Very handy, thanks. Is this standard or MS specific?
No, gcc also has it http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html
KIV
#pragmas are never standard as they are defined to be implementation-defined. However, GCC also supports them: http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html
Joey
+1  A: 

#pragma pack

user9876
A: 

for really specific structure alignments you can fiddle with padding bytes

So add a few dummy bytes between the various fields, until the alignment fits with your needs.

example:

struct example { unsigned short x; byte dummy1; byte dummy2; byte dummy3; byte dummy4; byte dummy5; byte dummy6; unsigned int y; };

if the dummy bytes wouldn't have been placed, the int would probably have been places on offset 4 (4 bytes from the beginning of the struct, while now it has been placed at offset 8)

waring: very compiler specific, and bad code practice ;^)

Toad