Search code examples
c++structalignment

Use the right alignment for a buffer which is supposed to hold a struct in C++


Suppose we have some struct, say

struct S
{
  double a, b;
  ~S(); // S doesn't have to be POD
};

Such a struct should typically have an alignment of 8, as the size of its largest contained type is 8.

Now imagine we want to declare a placeholder struct to hold the value of S:

struct Placeholder
{
  char bytes[ sizeof( S ) ];
};

Now we want to place it inside of another class:

class User
{
  char someChar;
  Placeholder holder;
public:
  // Don't mind that this is hacky -- this just shows a possible use but
  // that's not the point of the question
  User() { new ( holder.bytes ) S; }
  ~User() {  ( ( S * )( holder.bytes ) )->~S(); }
};

Problem is, Placeholder is now aligned incorrectly within User. Since the compiler knows that Placeholder is made of chars, not doubles, it would typically use an alignment of 1.

Is there a way to declare Placeholder with the alignment matching that of S in C++03? Note that S is not a POD type. I also understand C++11 has alignas, but this is not universally available yet, so I'd rather not count on it if possible.

Update: just to clarify, this should work for any S - we don't know what it contains.


Solution

  • I believe that boost::aligned_storage may be exactly what you're looking for. It uses the union trick in such a way that your type doesn't matter (you just use sizeof(YourType) to tell it how to align) to make sure the alignment works out properly.