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?

3 Answers3

6
#pragma pack

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

KIV
  • 745
  • 8
  • 12
  • No, gcc also has it http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html – KIV May 18 '09 at 18:02
  • #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 May 18 '09 at 18:03
1

#pragma pack

user9876
  • 10,954
  • 6
  • 44
  • 66
0

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
  • 15,593
  • 16
  • 82
  • 128