Jump to content
  • Advertisement

Archived

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

Radhil

Need some help - struct padding

This topic is 6023 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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!