Is there a clever way of avoiding extra padding with nested classes in C++?

Viewed 1058

These structs, align1 and align2, contain the same data, but align1 has more padding due to the nested layout. How can I get the memory saving alignment of align2 while also using a nested struct like in align1?

int main() {
    struct align1 {
        struct {
            double d;    // 8 bytes
            bool b1;    //+1 byte (+ 7 bytes padding) = 16 bytes
        } subStruct;
        bool b2;        //+1 byte (+ 7 bytes padding) = 24 bytes
    };
    struct align2 {
        double d;        // 8 bytes
        bool b1, b2;    //+2 byte (+ 6 bytes padding) = 16 bytes
    };

    std::cout << "align1: " << sizeof(align1) << " bytes\n";    // 24 bytes
    std::cout << "align2: " << sizeof(align2) << " bytes\n";    // 16 bytes

    return 0;
}

The nested subStruct struct is needed since it is going to be declared/defined outside. I'm using C++17 and Visual Studio 2017.

The resulting code can be as dirty or bad looking as hell. I just don't want it to throw random errors at me later on or break when changing the configuration.

5 Answers

I explicitly rely on the permission to propose code which is "dirty or bad looking as" ... anything. To be even more clear, I only provide an idea. You need to test yourself and take responsibility yourself. I consider this question to explicitly allow untested code.

With this code:

typedef union
{
    struct
    {
        double d;   // 8 bytes
        bool b1;    //+1 byte (+ 7 bytes padding) = 16 bytes
    } nested;
    struct
    {
        double d;       // 8 bytes
        bool b1, b2;    //+2 byte (+ 6 bytes padding) = 16 bytes
    } packed;
} t_both;

I would expect the following attributes/features:

  • contains the substruct as potentially typedefed elsewhere (can be used from an included header file)
  • substruct accessable as XXX.nested.d and XXX.nested.b1
  • at same address as XXX.packed
  • access to XXX.packed.b2 to what is considered padding within nested
  • both substructs have the same total size, which I hope means that even making arrays of this is OK

Whatever you do with this, it probably conflicts with the requirement that when writing and reading a union, then all read accesses must be to the same part of the union as the most recent write. Writing one and reading the other would hence not be strictly allowed. That is what I consider unclearn about this code proposal. That said, I have often used this kind of unions in environments for which the respective construct has explicity been tested.

In order to illustrate here is a functionally identical and also equally unclean version, which better illustrates that the substruct can be typdefed elsewhere:


/* Inside an included header "whatever.h" : */
typedef struct
{
    double d;   // 8 bytes
    bool b1;    //+1 byte (+ 7 bytes padding) = 16 bytes
} t_ExternDefedStruct;
/* Content of including file */

#include "whatever.h"

typedef union
{
    t_ExternDefedStruct nested;
    struct
    {
        double d;       // 8 bytes
        bool b1, b2;    //+2 byte (+ 6 bytes padding) = 16 bytes
    } packed;
} t_both;

With #pragma pack(push, 1) and some manual padding, you can get them to be the same.

#include <iostream>

int main() {
#pragma pack(push, 1)
    struct align1 {
        struct {
            double d;   // 8 bytes
            bool b1;    //+1 byte (+ 0 bytes padding) = 9 bytes
        } subStruct;
        bool b2;        //+1 byte (+ 0 bytes padding) = 10 bytes
        char pad_[6];   //+6 bytes (+ 0 bytes padding) = 16 bytes 
    };
#pragma pack(pop)
    struct align2 {
        double d;       // 8 bytes
        bool b1, b2;    //+2 byte (+ 6 bytes padding) = 16 bytes
    };

    std::cout << "align1: " << sizeof(align1) << " bytes\n";    // 16 bytes
    std::cout << "align2: " << sizeof(align2) << " bytes\n";    // 16 bytes

    return 0;
}

Output:

align1: 16 bytes
align2: 16 bytes

When your problem is padding, your answer is #pragma pack.

#pragma pack effect

It works on MSVC (where it was invented) and also on GCC (where it was added for compatibility with MSVC codebase).

Note that messing with alignment can end very badly. Putting a multi-byte members on odd (for them) bytes will result in run-time slowdowns. That is, in a good case scenario when your CPU supports unaligned operations at all. In a bad case, it crashes altogether (AFAIK trying to feed them to SSE instructions or non-x86, RISC CPUs).

The only legitimate use for #pragma pack(1) I personally know is mapping binary files straight to structs, especially headers of bitmap formats like BMPs (BITMAPINFOHEADER from wingdi.h) or TGAs. The other would be really big data structures, like gigabyte-sized arrays that @Arty mentioned.

In the bigger picture, padding is a time-memory trade-off. The CPU time saved on accessing a nicely aligned variable is well worth the wasted bytes, in overwhelming majority of cases. You need a really good reason to change that, because you're unlikely to come ahead without serious profiling of both approaches.

I implemented the following universal macros for packing structures in all popular compilers:

#if defined(_MSC_VER)
    #define ATTR_PACKED
    #define PACKED_BEGIN __pragma(pack(push, 1))
    #define PACKED_END __pragma(pack(pop))
#else
    #define ATTR_PACKED __attribute__((packed))
    #define PACKED_BEGIN
    #define PACKED_END
#endif

Put line PACKED_BEGIN before the outer struct, PACKED_END after outer struct, plus ATTR_PACKED word after struct word (before struct name) in all structures (including inner). All such marked structures will be densely packed the same way by all popular compilers, packed into the minimal possible size. See the code below; both of your structures will be aligned same way, both 10 bytes in size.

It was tested on online C++ compilers (click-open the following links to see tests): MSVC, GCC, and CLang.

If it is packed too densely you may add extra padding fields wherever needed, between or after fields, fields like char pad0[2]; and char pad1[3]; to insert 2 and 3 extra padding bytes.

#if defined(_MSC_VER)
    #define ATTR_PACKED
    #define PACKED_BEGIN __pragma(pack(push, 1))
    #define PACKED_END __pragma(pack(pop))
#else
    #define ATTR_PACKED __attribute__((packed))
    #define PACKED_BEGIN
    #define PACKED_END
#endif

#include <iostream>

int main() {
    PACKED_BEGIN
    struct ATTR_PACKED align1 {
        struct ATTR_PACKED {
            double d;
            bool b1;
        } subStruct;
        bool b2;
    };
    PACKED_END

    PACKED_BEGIN
    struct ATTR_PACKED align2 {
        double d;
        bool b1, b2;
    };
    PACKED_END

    std::cout << "align1: " << sizeof(align1) << " bytes\n";
    std::cout << "align2: " << sizeof(align2) << " bytes\n";

    return 0;
}

Output:

align1: 10 bytes
align2: 10 bytes
Related