title: Aligned memory allocation
body: <p>I have the following assignment:</p>
  
  <blockquote>
    <p>Write a function in C that allocates block of memory and returns a pointer to the start of the memory under these conditions:</p>
    
    <ol>
    <li>All addresses in the block are divisible by 32</li>
    <li>Allocated at least the number of the bytes required</li>
    <li>Every cell in the block is initialized to zero </li>
    <li>No global variants, minimum complexity</li>
    </ol>
    
    <p>Write another function that also releases the memory that you allocated. (You know that the aforesaid memory was allocated by the first function that you just wrote ).</p>
  </blockquote>
  
  <p>Here is a debugging temporary solution:</p>
  
  <p>"aligned_malloc.h":</p>
  
  <pre><code>#ifndef  __ALIGNED_MALLOC_H__
  #define  __ALIGNED_MALLOC_H__
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  
  #define ALIGNMENT 16
  
  #if ALIGNMENT &gt; 255
  #error "This 'void *aligned_malloc(size_t size)' function can only handle alignment &lt; 256"
  #endif
  
  #if ALIGNMENT &lt; 0
  #error "This 'void *aligned_malloc(size_t size)' function can only handle a non negative alignment"
  #endif
  
  void* aligned_malloc(size_t size);
  void aligned_free(void *aligned_p);
  
  #endif /* __ALIGNED_MALLOC_H__ */
  </code></pre>
  
  <p>"alligned_malloc.c":</p>
  
  <pre><code>#include "aligned_malloc.h"
  
  #undef  DEBUG
  #define DEBUG 1
  
  int main()
  {
      size_t size = 0;
      void *memblk = NULL;
  
      do while (size &gt; 0 )
      {
          printf("\nPlease enter in bytes the memory block size you want to allocate (0 to exit): ");
          scanf("%d", &amp;size);
          memblk = aligned_malloc(size);
  
          #ifdef     DEBUG
              printf("%s[%d]: memblk = 0x%x\n", __FUNCTION__, __LINE__, memblk);
          #endif  /* DEBUG */
  
      }
  
      if (memblk != NULL)
          aligned_free(memblk);
  
      return 0;
  }
  
  void *aligned_malloc(size_t size)
  {
      int i = 0;
      void* memblk = malloc(size + ALIGNMENT);
      printf ("\n%ld",(long)memblk);
  
      //offset is the start address of memblk (long=8digit) modulo the ALIGNMENT
      unsigned char offset = ALIGNMENT - ((long)memblk % ALIGNMENT);
      printf ("\n%d",offset);
  
      if (memblk == NULL)
          return NULL;
      /* O/W: */  
      #ifdef     DEBUG
      printf("%s[%d]: memblk = 0x%x\n", __FUNCTION__, __LINE__, memblk);
      #endif  /* DEBUG */
      for (i = offset; i &lt; size + ALIGNMENT; i++) 
      {
          printf("%02d: %08d\n", i, memblk+i);
          memblk[i] = 0;
      }
  
      *(memblk + offset - 1) = offset;
  
      return (void*)(memblk + offset);
  }
  
  void aligned_free(void *aligned_p)
  {
      unsigned char offset = (unsigned char)(*((unsigned char *)aligned_p-1));
      #ifdef     DEBUG
      printf("%s[%d]: offset = %d\n", __FUNCTION__, __LINE__, offset);
      printf("%s[%d]: aligned_p-1 = 0x%x\n", __FUNCTION__, __LINE__, (unsigned char *)aligned_p-1);
      printf("%s[%d]: aligned_p-offset = 0x%x\n", __FUNCTION__, __LINE__, aligned_p-offset);
      #endif  /* DEBUG */
      free(aligned_p-offset);
  }
  </code></pre>
  
  <p>Now I have several problems here:</p>
  
  <ol>
  <li>There is a compilation error in <code>main()</code></li>
  <li>I cannot catch how to use <code>calloc</code> instead of <code>malloc</code></li>
  <li>If I use the <code>void*</code> is there an option to cast it to 32 bytes chunks?</li>
  </ol>