title: C malloc crashing only on second call
body: <p>I'm having some trouble with a malloc call. The thing is that this call is on a function that I call twice on my program. <strong>The second malloc of the function crashes only on the second time that I call this function</strong>. I tried swapping the order of some of then and even delleting one.. In the first case, the second malloc (originally the first) was crashed, and in the second case, it crashed a realloc that appears later in the same function. Also, I tried calling my function one time before the second and kept crashing on the (new) second call. Can anyone help me? Here is my code: </p>
  
  <pre><code>int main(int argv, char *argc[]){
      fsys = malloc(sizeof(struct ext2system)); // Global pointer var
  
      getsysdata();
  
      list_dir(fsys-&gt;root);
  //  list_dir(fsys-&gt;root); // IF THIS LINE IS UNCOMMENTED, 
                            // THE PROGRAM CRASHES ON THIS CALL
                            // IF NOT, IT CRASHES ON data = get_cont(fsys-&gt;root); 
  
      pdir dir = malloc(sizeof(struct s_direct));
      int* data;
      int offs, i;
  
  
      data = get_cont(fsys-&gt;root);  
      offs = 0;
      for (i = 0; i &lt; fsys-&gt;root-&gt;i_links_count + 2; ++i) {
          offs += readdirent(getblock(data[0])+offs, dir);
          printf("%.*s\n", dir-&gt;name_len, dir-&gt;name);
          if(dir-&gt;file_type==1) printf("%s\n", data);
      }
  
      unmap(fsys-&gt;diskmap);
      return 0;
  }
  
  void list_dir(pinode inod){
      // Lists a directory contents
      pdir dir = malloc(sizeof(struct s_direct));
      int* data;
      int offs, i;
  
      data = get_cont(inod);
      offs = 0;
      for (i = 0; i &lt; inod-&gt;i_links_count + 2; ++i) {
          offs += readdirent(getblock(data[0])+offs, dir);
          printf("%.*s\n", dir-&gt;name_len, dir-&gt;name);
      }
  
  }
  
  int *get_cont(pinode inod){
      // Recupera contenido de los blocks de datos de un inodo
      int *cont=NULL; 
      int *idx=NULL;
      int i=0;
      int *block;
  
      cont = malloc(sizeof(int));
      idx  = malloc(sizeof(int)); // HERE IS WHERE THE PROGRAM CRASHES
                                  // EVEN IF MALLOCS ARE SWAPPED
  
      while(i &lt; inod-&gt;i_blocks &amp;&amp; i&lt;13) {
          // Recupera los 12 primeros bloques directamente
              realloc(cont, i*sizeof(int)); // CRASHED HERE WHEN
                                            // I DELETED ONE MALLOC
  
              cont[i]=inod-&gt;i_block[i];
              i++;
      }
  
      if(i &lt; inod-&gt;i_blocks){
          *idx=13;
          block=(int*)getblock(inod-&gt;i_block[*idx]);
          fetchcont(block, idx, cont, inod-&gt;i_blocks, 0);
      }
      if(i &lt; inod-&gt;i_blocks){
          block=(int*)getblock(inod-&gt;i_block[*idx]);
          fetchcont(block, idx, cont, inod-&gt;i_blocks, 1);
      }
      if(i &lt; inod-&gt;i_blocks){
          block=(int*)getblock(inod-&gt;i_block[*idx]);
          fetchcont(block, idx, cont, inod-&gt;i_blocks, 2);
      }
  
      return cont;
  
  }
  </code></pre>
  
  <p>Thanks in advice!</p>