Jump to content
  • Advertisement

Archived

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

Eric

question about C++ structures

This topic is 6624 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 this structure: struct TERRCOORD { WORD height; BYTE lightIntensity; }; So 3 bytes, right? But it turns out sizeof(TERRCOORD) evaluates to 4. I guess the WORD has to align to an even byte or something... right? In my game, this structure is used like so: DWORD terrainSize = 4000000; // or similar TERRCOORD* terrCoords = new TERRCOORD[ terrainSize ]; I really need to keep this data (height & lightIntensity) to 3 bytes/coordinate. How can I do this? I don''t want to have separate height and lightIntensity arrays, because then I''d have to incrememnt two pointers instead of one when processing the coords.

Share this post


Link to post
Share on other sites
Advertisement
If you are doing this in windoze w ith VC++ then it pads structures to be aligned on a 32 bit boundry by default. Fool around with the options and see if you can somehow disable padding.

---
www.crazycrackerz.org

Share this post


Link to post
Share on other sites
In MSVC, you can disable padding as follows:

    
// Byte-align, other values are 2, 4, 8, 16.
#pragma pack(1)
typedef struct
{
. . .
};

// Use default padding.

#pragma pack


But usually, you want to leave padding enabled. If a non-naturally sized piece of data is accessed, the processor will do multiple aligned memory accesses to assemble the data.

Edited by - mhkrause on June 26, 2000 10:16:43 PM

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!