Thread: Detecting Structure Padding

  1. #1
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186

    Detecting Structure Padding

    I'm creating a header file (to be included in DLL projects) that allows DLLs to communicate with the larger, main application. The header file declares a struct, and I've figured that the structure must be packed to ensure that different compilers don't insert padding in different places, leading to a situation in which the application and the DLLs (built with different compilers) have separate views of the same struct definition.

    However, different compilers have varying ways of indicating that a structure should be packed (VC++'s pragma directive as opposed to MingW's __attribute__); is there a way to detect which compiler is being used so the correct directive may be invoked?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    For VC++ it is
    Code:
    #ifdef _MSC_VER
    //Microsoft compiler
    #endif
    For gnu c/c++ it is
    Code:
    #ifdef __GNUC__
    //GNU compiler
    #endif
    gg

  3. #3
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Thanks. The information I found is as follows:

    Microsoft VC++
    _MSC_VER
    #pragma pack(1) / #pragma pack(pop)

    Borland C++
    _BORLANDC_
    #pragma pack(1) / #pragma pack()

    GNU C++
    __GNUC__
    __attribute__((packed))
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Structure Padding
    By audinue in forum C Programming
    Replies: 20
    Last Post: 07-12-2011, 10:14 PM
  2. How to handle structure padding
    By cdalten in forum C Programming
    Replies: 8
    Last Post: 03-17-2006, 09:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 05:56 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM