title: How to reallocate character pointer for concatenation?
body: <p>I wrote the function to tokenize the entire string and concatenate space and string length of each token . my functions is</p>
  
  <p>main function</p>
  
  <pre><code>char *final_buff = NULL;
  
  data_token(databuf,&amp;final_buff);
  </code></pre>
  
  <p>after that I call</p>
  
  <pre><code>free(final_buff);
  </code></pre>
  
  <p>function:</p>
  
  <pre><code>int data_token(char *buffer,/*user_strinf*/
                      char **final_buff)/*store each token*/
  {
      char *token_arr;
      char temp[10];
      int length = 0;
      token_arr = strtok(buffer,",");/*find first token*/
      while (token_arr != NULL)
      {
              printf("tokens--%s\n",token_arr);
  
              length = length + strlen(token_arr)+4;
  
              *final_buff = realloc(*final_buff,(length)*sizeof(char));/*allocate memory for the buffer*/
  
              if (NULL == *final_buff)
              {
                      printf("token memory allocation error\n");
                      exit(1);
              }
  
              strcat(*final_buff,token_arr);/*concatinate the token to buffer*/
              strcat(*final_buff," ");
              sprintf(temp,"%d",strlen(token_arr));
              strcat(*final_buff,temp);       /*concatinate buffer with string length */
              strcat(*final_buff," ");
              token_arr = strtok(NULL , ",");/*read next token */
      }
  
      return 1;
     }
  </code></pre>
  
  <p>is this allocation will work when I calling this function . Because I am scared about how much length should I allocate when adding space and integer with that array . is it correct ? I got segmentation fault or core dumped .</p>