title: preventing unaligned data on the heap
body: <p>I am building a class hierarchy that uses SSE intrinsics functions and thus some of the members of the class need to be 16-byte aligned. For stack instances I can use <code>__declspec(align(#))</code>, like so:</p>
  
  <pre><code>typedef __declspec(align(16)) float Vector[4];
  class MyClass{
  ...
  private:
  Vector v;
  };
  </code></pre>
  
  <p>Now, since <code>__declspec(align(#))</code> is a compilation directive, the following code may result in an unaligned instance of Vector on the heap:</p>
  
  <pre><code>MyClass *myclass = new MyClass;
  </code></pre>
  
  <p>This too, I know I can easily solve by overloading the <em>new</em> and <em>delete</em> operators to use <code>_aligned_malloc</code> and <code>_aligned_free</code> accordingly. Like so:</p>
  
  <pre><code>//inside MyClass:
  public:
  void* operator new (size_t size) throw (std::bad_alloc){
      void * p = _aligned_malloc(size, 16);
      if (p == 0)  throw std::bad_alloc()
      return p; 
  }
  
  void operator delete (void *p){
      MyClass* pc = static_cast&lt;MyClass*&gt;(p); 
      _aligned_free(p);
  }
  ...
  </code></pre>
  
  <p>So far so good.. but here is my problem. Consider the following code:</p>
  
  <pre><code>class NotMyClass{ //Not my code, which I have little or no influence over
  ...
  MyClass myclass;
  ...
  };
  int main(){
      ...
      NotMyClass *nmc = new NotMyClass;
      ...
  }
  </code></pre>
  
  <p>Since the myclass instance of <code>MyClass</code> is created statically on a dynamic instance of NotMyClass, myclass WILL be 16-byte aligned relatively to the beginning of nmc because of Vector's <code>__declspec(align(16))</code> directive. But this is worthless, since nmc is dynamically allocated on the heap with NotMyClass's <em>new</em> operator, which doesn't nesessarily ensure (and definitely probably NOT) 16-byte alignment.</p>
  
  <p>So far, I can only think of 2 approaches on how to deal with this problem:</p>
  
  <ol>
  <li><p>Preventing MyClass users from being able to compile the following code:</p>
  
  <pre><code>MyClass myclass;
  </code></pre>
  
  <p>meaning, instances of MyClass can only be created dynamically, using the new operator, thus ensuring that all instances of MyClass are truly dynamically allocatted with MyClass's overloaded new. I have consulted on another thread on how to accomplish this and got a few great answers:
  <a href="https://stackoverflow.com/questions/3092198/c-preventing-class-instance-from-being-created-on-the-stack-during-compiltaio">C++, preventing class instance from being created on the stack (during compiltaion)</a></p></li>
  <li><p>Revert from having Vector members in my Class and only have pointers to Vector as members, which I will allocate and deallocate using <code>_aligned_malloc</code> and <code>_aligned_free</code> in the ctor and dtor respectively. This methos seems crude and prone to error, since I am not the only programmer writing these Classes (MyClass derives from a Base class and many of these classes use SSE).</p></li>
  </ol>
  
  <p>However, since both solutions have been frowned upon in my team, I come to you for suggestions of a different solution.</p>