title: How can I align a buffer on the stack?
body: <p>I'm trying to achieve a memory buffer that's aligned by it's size so I can use the modulo feature of a DMA controller to implement a ring buffer.  I know I can do this with memalign, but I'm wondering if its possible to do so on the stack as so far I've been able to <strong>avoid dynamic memory</strong>.  I'm using GCC 4.4.1 and I don't care about portability (embedded system).</p>
  
  <p>I want to do something like:</p>
  
  <pre><code>template&lt;uint16_t num_channels, uint16_t buffer_size&gt;
  class sampler {
      __attribute__((aligned(buffer_size * num_channels * 2)))
      uint16_t buffer[buffer_size][num_channels];
  };
  </code></pre>
  
  <p>but of course GCC won't accept non-constant alignment (and seems to indicate that alignments > 8 may not be honored anyway).</p>
  
  <p>I think I could use C++0x alignas() to achieve this, but it doesn't seem to appear in GCC until version 4.8.</p>
  
  <p>I guess one option might be to double the size of the buffer, but that seems to waste a bunch of space (I'm planning on trying to use a substantial fraction of the device memory for this buffer).  Maybe I should just give up and use dynamic memory.  Is memalign going to be relatively efficient in terms of wasted space?</p>
  
  <p>Any ideas?</p>