1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Rep Power

    Memory aligning a struct

    I've just started to figure out the file format of a undocumented file, and I have a general struct like so:

    typedef struct fileheader {
       unsigned int number;
       char name[30];
       unsigned int unknown;
       char path[15];
    } fileheader;

    But when I try and use my C code to run it through it gets off by 3 bytes, because the sizeof this struct in memory is 56, where in the file its only 53.

    So when I try and do this
    files = (fileheader *)malloc(sizeof(fileheader) * numberOfFiles);
    ret = ReadFile(hFile, files, numberOfFiles, &read, NULL);
    It reads them wrong...

    and I know its because its to do with memory alignment, and I'm sure I remember seeing a way to tell my struct to align on byte boundaries instead of 4 byte boundaries...

    Could anyone point me in the correct direction for what I need to do to tell my struct to be smaller.

    I guess also I can solve this problem by reading each element one at a time, and just storing it bigger in memory… but I’m lazy, and I want to learn something new :)


    P.S Google didn't help me much, I found something about align in the MSDN, but it seemed very M$ specific, I was hoping for a general solution...
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    in the perfect world
    Rep Power
    have you looked at


    specifically the pack commands



    ie /Zp1
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Flint, MI
    Rep Power
    Trying to force memory alignment is a bad idea. A better idea would be to read in the 53 byte buffer, then assign the components of the record to the relevant members of the struct. You're getting the problems that you have right now because somebody relied on memory alignment before.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Rep Power
    Structure packing is always compiler dependent, no particular packing is enforced by the ANSI/ISO standard. The default packing is usually chosen for performance reasons. Each compiler has its own proprietary methods for defining packing. MSVC++ uses #pragma or command line options, GCC uses the __attribute__ extension.

    The only portable way of doing it is to read the data in as a character array, and convert each field separately and assign it to the structure member, and vice versa for writing (de-serialisation/serialisation). Remember that byte ordering may also be an issue if the code is ported to another processor architecture (powerPC for example).

    Last edited by clifford; September 17th, 2003 at 08:12 AM.

IMN logo majestic logo threadwatch logo seochat tools logo