could someone please enlighten me as to why realloc() keeps crashing my program? the function definitely works, but it seems to only work for a randomly small number of times before it craps out. i've read the man page probably 20 times, as well as googling for help, but i can't seem to see any reason for this behavior. thanks in advance!Code:char *str_cat(char *s1, const char *s2)
{
if ((s1 = (char *)realloc(s1, strlen(s1) + strlen(s2) + 1)) == NULL) {
perror("mem.c: str_cat(): realloc()");
return NULL;
}
strcat(s1, s2);
return s1;
}
p.s. 's1' has been allocated (or reallocated) before every call.