title: Why won't realloc work in this example?
body: <p>My professor gave us an "assignment" to find why <code>realloc()</code> won't work in this specific example. 
  I tried searching this site and I think that it won't work because there is no real way to determine the size of a memory block allocated with <code>malloc()</code> so <code>realloc()</code> doesn't know the new size of the memory block that it needs to reallocate.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;windows.h&gt;
  int main ()
  {
      MEMORYSTATUS memInfo;
      memInfo.dwLength = sizeof(MEMORYSTATUS);
      GlobalMemoryStatus(&amp;memInfo);
      double slobodno = memInfo.dwAvailVirtual/1024./1024.;
  
      printf("%g MB\n",slobodno);
      int br=0,i,j;
  
      char **imena,*ime,*temp,*bbb=NULL;
      imena=(char**) malloc(sizeof(char*)*(br+1));
      while(1)
      {
          printf("Unesite ime: ");
          ime=(char*) malloc(sizeof(char)*4000000);
          gets(ime);
          printf("%u\n", strlen(ime));
          ime=(char*) realloc(ime,strlen(ime)+1);
  
          GlobalMemoryStatus(&amp;memInfo);
          slobodno = memInfo.dwAvailVirtual/1024./1024.;
          printf("%g MB\n",slobodno);
  
          if (strcmp(ime,".")==0)
             {free(ime);free(imena[br]);break;}
          imena[br++]=ime;
          imena=(char**) realloc(imena,sizeof(char*)*(br+1));
      }
  
      for (i=0;i&lt;br-1;i++)
          for (j=i+1;j&lt;br;j++)
              if (strcmp(imena[i],imena[j])&gt;0)
              {
                  temp=imena[i];
                  imena[i]=imena[j];
                  imena[j]=temp;
              }
  
  
      //ovde ide sortiranje
      for (i=0;i&lt;br;i++)
          printf("%s\n",imena[i]);
  
      for(i=0;i&lt;br;i++)
         free(imena[i]);
  
      free(imena);
  
      return 0;
  }
  </code></pre>
  
  <p>Note: Professor added the lines for printing out the available memory so we can see that <code>realloc()</code> doesn't work. Every new string we enter just takes up <code>sizeof(char)+4000000</code> bytes and can't be reallocated. I'm trying to find out why. Thanks in advance</p>