Original Post
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]