title: Aligned memory management?
body: <p>I have a few related questions about managing aligned memory blocks.  Cross-platform answers would be ideal.  However, as I'm pretty sure a cross-platform solution does not exist, I'm mainly interested in Windows and Linux and to a (much) lesser extent Mac OS and FreeBSD.</p>
  
  <ol>
  <li><p>What's the best way of getting a chunk of memory aligned on 16-byte boundaries?  (I'm aware of the trivial method of using <code>malloc()</code>, allocating a little extra space and then bumping the pointer up to a properly aligned value.  I'm hoping for something a little less kludge-y, though.  Also, see below for additional issues.)</p></li>
  <li><p>If I use plain old <code>malloc()</code>, allocate extra space, and then move the pointer up to where it would be correctly aligned, is it necessary to keep the pointer to the beginning of the block around for freeing?  (Calling <code>free()</code> on pointers to the middle of the block seems to work in practice on Windows, but I'm wondering what the standard says and, even if the standard says you can't, whether it works in practice on all major OS's.  I don't care about obscure <a href="http://www.worldlingo.com/ma/enwiki/en/DeathStation_9000">DS9K</a>-like OS's.)</p></li>
  <li><p><strong>This is the hard/interesting part</strong>.  What's the best way to reallocate a memory block while preserving alignment?  Ideally this would be something more intelligent than calling <code>malloc()</code>, copying, and then calling <code>free()</code> on the old block.  I'd like to do it in place where possible.</p></li>
  </ol>