Thread: Ok, I'm dumb, byte alignment for the third time

  1. #1

    Ok, I'm dumb, byte alignment for the third time

    salem let me explain, I went to delete the FIRST thread I made on byte alignment after I realized I posted two threads on the same topic (I forgot I made the first). Then, I couldn't find the most recent one I made so I assume you deleted it, now none exist but I still need to understand what this means.

    Anyway I know that PACK_STRUCT expands to nothing, but I don't know what the rest of those '#' directives mean, and I would like someone to tell me. I don't know what the pack/push __attribute__((packed)) lines mean. When I just include the structures without the defines the application doesn't work properly.

    #ifdef _MSC_VER
    #	pragma pack( push, packing )
    #	pragma pack( 1 )
    #	define PACK_STRUCT
    #elif defined( __GNUC__ )
    #	define PACK_STRUCT	__attribute__((packed))
    #	error you must byte-align these structures with the appropriate compiler directives
    typedef unsigned char byte;
    typedef unsigned short word;
    // File header
    struct MS3DHeader
    	char m_ID[10];
    	int m_version;
    // Default alignment
    #ifdef _MSC_VER
    #	pragma pack( pop, packing )
    #undef PACK_STRUCT
    yeah well salem don't beat me up plz

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Its all explained in the msvc helpfiles and also at msdn i guess.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    Registered User
    Join Date
    Aug 2002
    Hermosa Beach, CA
    This all has to do with the way different compiler makers chose to deal with the alignment issue. Microsoft chose to use #pragmas, and to use them you have to specify the point where you should start a particular alignment, and where you should end it (the code you give is single byte alignment). That is the #pragma push & pop code, and it is defined to only be compiled for Microsoft.
    The gcc compiler uses a different approach: the __attribute__((packed)) stuff. Under that approach, you tag a particular structure to be packed (rather than a block of code). I think I read once in the gcc documenation a statement to the effect that pragmas are never a good idea, and that is why they didnt use them. Right or wrong, that was their approach to solving the alignment issue.

    The code block you showed was code that was conditionally defined to work on either gcc (__GNUC__) or Microsoft VC (_MSC_VER).

    Hopefully that clears up something.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM