Archived

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

Need some help - struct padding

This topic is 5743 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

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

// 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