title: realloc overwrite variable (Xilinx SDK on a Zynq SoC (Cortex A9))
body: <p>As mentioned I have a Zynq SoC (ZC706 Eval Board) and I'm trying to read an image from the SD Card. To do this I'm using the FatFs lib (<a href="http://elm-chan.org/fsw/ff/00index_e.html" rel="nofollow">http://elm-chan.org/fsw/ff/00index_e.html</a>).
  In my code I read 4096 Byte from the file and save it to a buffer. After that i copy the buffer to an unsigned char pointer that size I increase after every read operation. </p>
  
  <p>Then I'm using realloc, the for loop in the copyU32ArrayToUnsignedCharArray function 'failed' because the size variable is overwritten by the out array.</p>
  
  <p>Code that overwrite the "size" in the copyU32ArrayToUnsignedCharArray function:</p>
  
  <pre><code>u32 buffer[1024];
  unsigned char *img = NULL;
  bytesreaded = 0;
  for (;;) {
      br=0;
      fr = f_read(&amp;fil, buffer, sizeof(buffer), &amp;br); /* Read a chunk of source file */
      if (fr || br == 0)
          break; /* error or eof */
  
      img = realloc(img,br);
      copyU32ArrayToUnsignedCharArray(buffer, &amp;img[bytesreaded], br/4); // /4 because u32(32 bit) in to unsigned char(8 bit)
      bytesreaded += br; // update readed bytes 
  }
  </code></pre>
  
  <p>The code that worked:</p>
  
  <pre><code>    u32 buffer[1024];
  unsigned char *img = NULL;
  img = malloc(512*512*3+100);
  bytesreaded = 0;
  for (;;) {
      br=0;
      fr = f_read(&amp;fil, buffer, sizeof(buffer), &amp;br); /* Read a chunk of source file */
      if (fr || br == 0)
          break; /* error or eof */
  
      copyU32ArrayToUnsignedCharArray(buffer, &amp;img[bytesreaded], br/4); // /4 because u32(32 bit) in to unsigned char(8 bit)
      bytesreaded += br; // update readed bytes 
  }
  </code></pre>
  
  <p>The copyU32ArrayToUnsignedCharArray function:</p>
  
  <pre><code>void copyU32ArrayToUnsignedCharArray(u32 *in, unsigned char* out, uint size){
  int i,x;
  
  x = 0;
  for (i = 0; i &lt; size; i++) {
      if(size != 1024)
          break;
      in[i] = Xil_In32BE(&amp;in[i]);
      out[x] = (u32) in[i] &gt;&gt; 24;
      out[x + 1] = (u32) in[i] &gt;&gt; 16 &amp; 0x00FF;
      out[x + 2] = (u32) in[i] &gt;&gt; 8 &amp; 0x0000FF;
      out[x + 3] = (u32) in[i] &amp; 0x000000FF;
      x += 4;
  }
  }
  </code></pre>
  
  <p>I want to use realloc because I don't know how big the image will be that I read.</p>
  
  <p><strong>Update:</strong><br>
  Some further information to the code that doesn't work. I debugged it and the pointer to *img isn't null, so the realloc was successfully. If I'm using gdb the following things happen in the copyU32ArrayToUnsignedCharArray function:<br>
   - pointer to the variable "out" is 0x001125a8<br>
   - the address of the "size" variable is 0x0011309c (the value that is stored at this location is correct)<br>
   - the space in memory between this two variables is 0xaf4 = 2804 dec (difference of the two addresses)<br>
   - if the for loop within the copyU32ArrayToUnsignedCharArray function reached i=702 and x=2808 the size variable is changed to another value  </p>
  
  <p>Sincerely,<br>
  Arno</p>