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 .