Archived

This topic is now archived and is closed to further replies.

Radhil

Need some help - struct padding

Recommended Posts

I have heard sometimes the compiler will pad structures with extra blank bytes in order to optimize memory access speed and all that. That''s fine and all, except I had no idea it inserted padding BETWEEN the members in a struct. It took me hours to figure this out, of course. My code relies on the byte-order and placement of the members, so obviously this screws things up. And I''d rather not rewrite everything I do to go member-by-member-by-member (blech). The question is - how do you turn this crap off? Either compiler-wide (VC++6), or force a specific structure''s integrity? Radhil Trebors Persona Under Construction

Share this post


Link to post
Share on other sites
Try wrapping the struct with #pragma pack directives. Read the compiler docs to get a better feel for them. If you lost them, try MSDN.

HTH

Share this post


Link to post
Share on other sites

// save current global packing size
#pragma pack(push)

// set packing to 1 byte boundaries (i.e. no packing)
#pragma pack(1)

// some structure which needs 1 byte alignment (i.e. no padding)
struct blah
{
BYTE a;
DWORD b;
} blah;

// restore global packing size
#pragma pack(pop)


--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Or,

#include <pshpackX.h>
struct S { ... };
#include <poppack.h>

where X is 1, 2, 4, or 8.

Or, in settings-c++-code generation-struct member alignment.

Share this post


Link to post
Share on other sites