Skip to main content
GameDev.net gamedev.net
🔒 Locked

Padding, Memory Access, and Disk space.

Started by TravisWells Oct 28, 2002 at 11:24 PM 2 replies 1k views
Original Post
TravisWells
TravisWells
I've got a little theory in me head, I'd like to see if wiser people agree with me. (Sorry about the length) I'm assuming some things here, I could be wrong. 1. I can force byte-aligned (aka not padded) like this: #pragma pack(push, 1) #pragma pack(pop) 2. If I have a non-padded struct, I can fread() or fwrite() it in one call, assuming I stay on the same compiler. (So I can't port to Linux or Mac) 3. Non-padded structs are slower, since they aren't aligned on 32/64/whatever boundaries. I'm assuming those are true for now. Now here's my like theory: Say I have a simple struct representing an object in my game. It only contains some long int's, and a float or two. No pointers/STL containers. (With a struct this simple, just writing each varible wouldn't be hard. But it's just an example ) I'm going to be using this struct a lot in my game, so I want it to be fast. I may have 10,000 of them in memory at once, so I need it to be fast. But I want to be able to save the games state, so I need to write them all to disk. I could fwrite() each varible one-by-one, but I'd like to just fwrite() it in one call, for speed and simplicity. Say I create a second struct, containing the same varibles (minus any that are generated at run-time, stuff that I don't need to store between games) but I use the #pragma (push,1) on this. So now I have a padded and a non-padded version of the struct. Could I just write a function to convert a padded struct instance to a non-padded instance, and then write the non-padded out to disk? (and one for reading) I know at this point, doing each varible one-by-one would be just as simple, but I want to know if it's possible. EDIT: fixed #pragma's. [edited by - TravisWells on October 28, 2002 12:45:27 AM]
Neosmyle
Neosmyle
Yes, you could have a padded version and a non-padded version. I''m not sure what you would gain from it, but it is possible. Assuming c++, you could just add a copy ctor and an overloaded operator= to each struct.

struct Obj;
struct PadObj;

struct Obj
{
const Obj &operator= (const PadObj &a);
Obj(const PadObj &a);
...
};

struct PadObj
{
const PadObj &operator= (const Obj &a);
PadObj(const Obj &a);
...
};

Then you could convert freely between the two.
TravisWells
TravisWells
I wouldn''t gain anything from it really (except simpler fwrite()''ing).
I''m just trying to fill in a gap in my C++ knowledge.
niyaw
niyaw
a better way of dealing with padding is rearranging your data so that padding is not needed or is minimal. for instance, the following bunch of data will not require padding:

struct data {
// 4 byte aligned
int a;
// 2 byte aligned, also 4 byte aligned
short b;
// also 2 byte aligned!
short c;
// 4 byte aligned
int d;
// 1 byte aligned x 2
char f;
char g;
// 2 byte aligned
short h;
};

that is because padding makes sure that every element of the struct is aligned to the smaller of the element type byte size and the packing size.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.