How to turn off fields alignment?



  • How to make platform-independent way to turn off members and structures alignment in some piece of code?

    In Visual Studio I use #pragma pack(1) before struct which needs to be packed, and #pragma pack() after it to restore original setting.



  • SAn schrieb:

    How to make platform-independent way to turn off members and structures alignment in some piece of code?

    In Visual Studio I use #pragma pack(1) before struct which needs to be packed, and #pragma pack() after it to restore original setting.

    this depends on which compiler you plan to port your code for. with the gcc compiler you might specifiy alignment with the __attribute__ keyword:

    struct foo
    {
        int x[2] __attribute__ ((aligned(8)));
    };
    

    see: linkage

    to make code portable with msvc i see no other option than using the preprocessor directives:

    #ifdef WIN32
    
    #else
    
    #endif
    

    but i might miss other possibilities 🙂


Anmelden zum Antworten