title: What alignment issues limit the use of a block of memory created by malloc?
body: <p>I am writing a library for various mathematical computations in C. Several of these need some "scratch" space -- memory that is used for intermediate calculations. The space required depends on the size of the inputs, so it cannot be statically allocated. The library will typically be used to perform many iterations of the same type of calculation with the same size inputs, so I'd prefer not to <code>malloc</code> and <code>free</code> inside the library for each call; it would be much more efficient to allocate a large enough block once, re-use it for all the calculations, then free it.</p>
  
  <p>My intended strategy is to request a <code>void</code> pointer to a single block of memory, perhaps with an accompanying allocation function. Say, something like this:</p>
  
  <pre><code>void *allocateScratch(size_t rows, size_t columns);
  void doCalculation(size_t rows, size_t columns, double *data, void *scratch);
  </code></pre>
  
  <p>The idea is that if the user intends to do several calculations of the same size, he may use the allocate function to grab a block that is large enough, then use that same block of memory to perform the calculation for each of the inputs. The allocate function is not strictly necessary, but it simplifies the interface and makes it easier to change the storage requirements in the future, without each user of the library needing to know exactly how much space is required.</p>
  
  <p>In many cases, the block of memory I need is just a large array of type <code>double</code>, no problems there. But in some cases I need mixed data types -- say a block of doubles AND a block of integers. My code needs to be portable and should conform to the ANSI standard. I know that it is OK to cast a <code>void</code> pointer to any other pointer type, but I'm concerned about alignment issues if I try to use the same block for two types.</p>
  
  <p>So, specific example. Say I need a block of 3 <code>double</code>s and 5 <code>int</code>s. Can I implement my functions like this:</p>
  
  <pre><code>void *allocateScratch(...) {
      return malloc(3 * sizeof(double) + 5 * sizeof(int));
  }
  
  void doCalculation(..., void *scratch) {
      double *dblArray = scratch;
      int *intArray = ((unsigned char*)scratch) + 3 * sizeof(double);
  }
  </code></pre>
  
  <p>Is this legal? The alignment probably works out OK in this example, but what if I switch it around and take the <code>int</code> block first and the <code>double</code> block second, that will shift the alignment of the <code>double</code>'s (assuming 64-bit doubles and 32-bit ints). Is there a better way to do this? Or a more standard approach I should consider?</p>
  
  <p>My biggest goals are as follows:</p>
  
  <ul>
  <li>I'd like to use a single block if possible so the user doesn't have to deal with multiple blocks or a changing number of blocks required.</li>
  <li>I'd like the block to be a valid block obtained by <code>malloc</code> so the user can call <code>free</code> when finished. This means I don't want to do something like creating a small <code>struct</code> that has pointers to each block and then allocating each block separately, which would require a special destroy function; I'm willing to do that if that's the "only" way.</li>
  <li>The algorithms and memory requirements may change, so I'm trying to use the allocate function so that future versions can get different amounts of memory for potentially different types of data without breaking backward compatibility.</li>
  </ul>
  
  <p>Maybe this issue is addressed in the C standard, but I haven't been able to find it.</p>