title: Realloc and strcpy
body: <p>My code gives me segfault error: which I don't understand,the debugger says error comes from printing the value from stored_ </p>
  
  <pre><code>char *stored_ = NULL;
  char testMessage[15];
  
  //strcpy(stored_, testMessage);
  
  for (int a = 0;a &lt; 10; a++)
  {
      sprintf(testMessage,"Message::%i\n",a);
      printf("string is:%s;length is %i\n",testMessage,strlen(testMessage));
  
      stored_ = (char*) realloc (stored_, sizeof(char) * (strlen(testMessage) * (a+1) ));
  
      strcpy(&amp;stored_[a], testMessage);
  } 
  
  for (int b = 0;b &lt; 10; b++)
  {
      printf("inside:|%s|\n",stored_[b]);
  }
  </code></pre>