Re: realloc(): invalid next size
- From: rlb@xxxxxxxxxxxxxxxxxxxxxx (Richard Bos)
- Date: Tue, 11 Apr 2006 15:59:56 GMT
Deephay <tudoxxx@xxxxxxxxx> wrote:
Richard Bos wrote:
Deephay <tudoxxx@xxxxxxxxx> wrote:
I have to get the index where the first "space" locates.p = min(strcspn(&data[m], " "),
strcspn(&data[m], "\t"),
strcspn(&data[m], "\r"),
strcspn(&data[m], "\n"));
Why not simply use strcspn(&data[m], " \t\r\n")?
That one call does the same thing as those four calls plus a properly
written min() function.
key = realloc(key, p);
strncpy(key, &data[m], p);
key[p] = '\0';
trans(key, p);
m = m + p - 1;
the error occurred after a few iterations, the first realloc will alwaysand the "key" is already malloced before the loop:
char *key = malloc(1);
Hard to say what the real problem is without any object or function
definitions, but two questions spring to mind:
- does this occur immediately, or after a few iterations;
- what actually is the value of p (and if this code isn't complete, of
key) at the point where it produces the error?
success.
the p is always larger than 0, I have tested. Actually it will be a
quite normal value where the error produced, say, 7, 8, or whatever...
What I think now is that it might be a bug of glibc...
That is the last thing I would consider.
I searched with google and found some unsolved problem like the one I have.
You can find a great many confused bug-writers on the 'web, yes, many of
them blaming their tools for their own mistakes.
What I did now is, declare a key[50] static, this works fine.
It probably works around a bug - using strncpy() without thorough
understanding is asking for one, btw - and leaves a time bomb in your
code. What happens if you encounter a token larger than 50 characters?
Worse, what if you encounter one of _precisely_ 50 characters?
I still want to make sure what the problem is, though.
Without more code which demonstrates the problem precisely, it is
impossible to be certain. If the above is your exact code, you have
several off-by-one errors; then again, if the above is your exact code,
it doesn't compile, because there is a lot missing.
If you want more help, what you should do now is the following:
- make a copy of your existing code;
- whittle it down to the smallest _compilable_ program which still
exhibits the same problem;
- post _exactly_ that code. Paste, do not retype it into your post,
otherwise you'll make typos.
Richard
.
- Follow-Ups:
- Re: realloc(): invalid next size
- From: tudoxxx
- Re: realloc(): invalid next size
- References:
- realloc(): invalid next size
- From: Deephay
- Re: realloc(): invalid next size
- From: Richard Bos
- Re: realloc(): invalid next size
- From: Deephay
- realloc(): invalid next size
- Prev by Date: Re: How to read strings from a file with comments
- Next by Date: Re: Malloc and recursion
- Previous by thread: Re: realloc(): invalid next size
- Next by thread: Re: realloc(): invalid next size
- Index(es):
Relevant Pages
|
|