realloc(): invalid next size



Greetings all,

I have a program that used the realloc() function to change the
allocated size of a buffer, the program works with some arguments, but
with some other arguments, it will show me the error message like:

*** glibc detected *** realloc(): invalid next size: 0x0804c3a8 ***

and then I inserted a perror("realloc") to see what happend, it says that:

realloc: Illegal seek
the realloc() is in a loop:

for (m = 0; m < len; m++) {
if (isspace(data[m]) || ispunct(data[m]) ||
isdigit(data[m]))
printf("%c", data[m]);
else {
p = min(strcspn(&data[m], " "),
strcspn(&data[m], "\t"),
strcspn(&data[m], "\r"),
strcspn(&data[m], "\n"));
key = realloc(key, p);
strncpy(key, &data[m], p);
key[p] = '\0';
trans(key, p);
m = m + p - 1;
}
}

and the "key" is already malloced before the loop:

char *key = malloc(1);

Any suggestion could be helpful, thx very much!

Deephay
.



Relevant Pages

  • Re: [C] please critique my code
    ... > Elliot Marks wrote: ... > string and no more. ... I know that calling realloc repeatedly in a loop is ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [C] please critique my code
    ... > realloc grow the binary string 1 byte at a time as long as there is ... allocate a fixed amount which may be much more than needed or not ... I know that calling realloc repeatedly in a loop is ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Anything wrong with this function?
    ... I've written a function similar to strdup(), ... end loop ... ... int redup ... If realloc returns NULL, you have lost the original value of *s1. ...
    (comp.lang.c)
  • Re: realloc(): invalid next size
    ... it will show me the error message like: ... the realloc() is in a loop: ... and the "key" is already malloced before the loop: ... the error occurred after a few iterations, ...
    (comp.lang.c)
  • Re: file into dynamic arrays
    ... int xl; ... Cords *cords; ... re-read the realloc() documentation. ... Work out how to terminate the loop. ...
    (comp.lang.c)