3

I have encountered so called cryptic realloc invalid next size error , I am using gcc on linux my code is

 #include<stdio.h>
 #include<stdlib.h>
 #include<string.h>

int main()
{
 int i;
 char *buf;
 char loc[120];
 buf = malloc(1);
 int size;

 for(i=0;i<1920;i++)
  {
    sprintf(loc,"{Fill_next_token = my_next_token%d; Fill_next_token_id = my_next_token_id = my_next_token_id%d}",i,i);
    size = strlen(buf)+strlen(loc);
    printf("----%d\n",size);

    if(!realloc(buf,size))
    exit(1);
    strcat(buf,loc);
    }
  }

(mine might be duplicate question) here the solution somewhere lies by avoiding strcat and to use memcpy , But in my case I really want to concatenate the string . Above code works for good for such 920 iterations but in case 1920 realloc gives invalid new size error. Please help to find alternative of concatenating , looking forward to be a helpful question for lazy programmers like me .

Community
  • 1
  • 1
Akaks
  • 461
  • 3
  • 21
  • possible duplicate of [using realloc in c](http://stackoverflow.com/questions/5322364/using-realloc-in-c) – 2501 Jan 07 '15 at 14:40
  • Please take care of the title when asking a question here. This is very low quality. – Jens Gustedt Jan 07 '15 at 14:48
  • I came here for help , anyways thank for the advice will keep in mind. – Akaks Jan 07 '15 at 15:03
  • *looking forward to be a helpful question for lazy programmers like me* That does **not** make me want to help – Degustaf Jan 07 '15 at 15:07
  • thanks @degustaf , the fix got in answer worked well but it may cause for performance issue while appending to string in 10^n nth iterations . can you still will not help. – Akaks Jan 07 '15 at 15:13

3 Answers3

8

Your code has several issues:

  • You are not accounting for null terminator when deciding on the new length - it should be size = strlen(buf)+strlen(loc)+1;
  • You are ignoring the result of realloc - you need to check it for zero, and then assign it back to buf
  • You do not initialize buf to an empty string - this would make the first call of strlen produce undefined behavior (i.e. you need to add *buf = '\0';)

Once you fix these mistakes, your code should run correctly:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
   int i;
   char *buf= malloc(1);
   *buf='\0';
   char loc[120];

   for(i=0;i<1920;i++) {
      sprintf(loc,"{Fill_next_token = my_next_token%d; Fill_next_token_id = my_next_token_id = my_next_token_id%d}",i,i);
      int size = strlen(buf)+strlen(loc)+1;
      printf("----%d\n",size);
      char *tmp = realloc(buf,size);
      if(!tmp) exit(1);
      buf = tmp;
      strcat(buf, loc);
   }
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Thanks , Initially i dint thought those fundamental things because this code was working for some 1200 iterations. I thought there must be something else either than starcat , because appending at 2000th iteration it had to go through around 150KB of characters . Anyways that code worked – Akaks Jan 07 '15 at 15:07
  • why using a tmp variable and not directly buf? – perrocallcenter Mar 18 '23 at 05:59
  • 1
    @perrocallcenter That's an excellent question! The reason you need to use a temporary is to be able to handle the situation when `realloc` returns `nullptr` more gracefully than simply calling an `exit(1)`. Without a `tmp` you'd be unable to `free` the memory block that you are re-allocating, because its address would be lost. – Sergey Kalinichenko Mar 18 '23 at 22:36
1

buf is not a valid string so strcat() will fail since it expects a \0 terminated string.

If you want to realloc() buf then you should assign the return value of realloc() to buf which you are not doing.

char *temp = realloc(buf,size+1);
if(temp != NULL)
buf = temp;
Gopi
  • 19,784
  • 4
  • 24
  • 36
0

Point 1. Always use the return value of realloc() to access the newly allocated memory.

Point 2. strcat() needs a null-terminating string. Check the first iteration case.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261