title: How can realloc not work but malloc can?
body: <p>I have reached a point where realloc stops returning a pointer - I assume that there is a lack of space for the array to expand or be moved. The only problem is I really need that memory to exist or the application can't run as expected, so I decided to try malloc - expecting it not work since realloc would no work - but it did. Why?</p>
  
  <p>Then I memcpy the array of pointers into the new allocated array, but found it broke it, pointers like 0x10 and 0x2b was put in the array. There are real pointers, but if I replace the memcpy with a for loop, that fixes it. Why did memcpy do that? Should I not be using memcpy in my code?</p>
  
  <p>Code:</p>
  
  <pre><code>float * resizeArray_by(float *array, uint size)
  {
      float *tmpArray = NULL;
      if (!array)
      {
          tmpArray = (float *)malloc(size);
      }
      else
      {
          tmpArray = (float *)realloc((void *)array, size);
      }
  
      if (!tmpArray)
      {
          tmpArray = (float *)malloc(size);
          if (tmpArray)
          {
              //memcpy(tmpArray, array, size - 1);
              for (int k = 0; k &lt; size - 1; k++)
              {
                  ((float**)tmpArray)[k] = ((float **)array)[k];
              }
              free(array);
          }
      }
  
      return tmpArray;
  }
  
  void incrementArray_andPosition(float **&amp; array, uint &amp;total, uint &amp;position)
  {
      uint prevTotal = total;
      float *tmpArray = NULL;
      position++;
      if (position &gt;= total)
      {
          total = position;
          float *tmpArray = resizeArray_by((float *)array, total);
          if (tmpArray)
          {
              array = (float **)tmpArray;
  
              array[position - 1] = NULL;
          }
          else
          {
              position--;
              total = prevTotal;
          }
      }
  }
  
  void addArray_toArray_atPosition(float *add, uint size, float **&amp; array, uint &amp;total, uint &amp;position)
  {
      uint prevPosition = position;
      incrementArray_andPosition(array, total, position);
  
      if (position != prevPosition)
      {
          float *tmpArray = NULL;
          if (!array[position - 1] || mHasLengthChanged)
          {
              tmpArray = resizeArray_by(array[position - 1], size);
          }
  
          if (tmpArray)
          {
              memcpy(tmpArray, add, size);
              array[position - 1] = tmpArray;
          }
      }
  }
  </code></pre>
  
  <p>After all my fixes, the code inits probably. The interesting thing here, is after sorting out the arrays, I allocate with malloc a huge array, so to reorder the arrays into  one array to be used as an GL_ARRAY_BUFFER. If realloc is no allocating because of a lack of space, then why isn't allocating?</p>
  
  <p>Finally, this results it crashing in the end anyway. After going through the render function once it crashes. If I removed all my fixes and just caught when realloc doesn't allocate it would work fine. Which begs the question, what is wrong with mallocing my array instead of reallocing to cause so problems further down the line?</p>
  
  <p>My Array's are pointer of pointers of floats. When I grow the array it is converted into a pointer to floats and reallocated. I am building on Android, so this is why I assumed there to be a lack of memory. </p>