Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: jt@xxxxxxxxxxx (Jens Thoms Toerring)
- Date: 16 Jul 2007 19:59:16 GMT
gert <gert.cuykens@xxxxxxxxx> wrote:
BINGO 2 :)
PS any memory leaks in there ?
Sorry, but you're not done yet;-)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void add(char ***page, char *line, size_t n)
{
char **temp=*page;
if (temp = (char**) realloc(temp, sizeof(char*) * (n + 1)))
Do the calculations: you pass n = 0 to the function, so you
here allocate just a single char pointer.
{
temp[n] = line;
This is still fine.
temp[n+1] = NULL;
But here you assign NULL to something you haven't allocated,
you would need space for two pointers. And that's something
you definitely aren't allowed to do. It may look as every-
thing works but you have a bug there that may lead to your
program crashing (or even worse, giving you strange results)
at any time.
*page = temp;
}
You should always take into consideration the case that the
memory allocation fails, so in a "real" program you would
have to deal with that case here.
}
int main(int argc, char **argv)
{
size_t i;
char *line;
char *line1 = "test1";
char **page = NULL;
add(&page, line1, 0);
for(i=0;line=page[i];i++)
{
printf("%s",line);
}
return 0;
}
If you want to always have a NULL as the last element of the
array of char pointers then it's probably reasonable to allo-
cate memory for that at the very start of your program.
You could e.g. have a function (error checking left as an
exercise to the reader;-):
char **init( void ) {
char **p = malloc( sizeof *p );
p[ 0 ] = NULL;
return p;
}
and then use it in main() (again without error checking!) as
int main( int argc, char **argv )
{
size_t i;
char *line;
char *line1 = "test1";
char **page = init( );
...
}
Now change your add() function to reallocate (n + 2) times
the size of an char pointer (instead of just (n + 1) and
things (hopefully) should work correctly.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.
- Follow-Ups:
- References:
- *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: gert
- Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: mark_bluemel
- Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: Chris Dollin
- Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: gert
- Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: Jens Thoms Toerring
- Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- From: gert
- *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- Prev by Date: Re: sin (M_PI)
- Next by Date: Re: comparing mantissa
- Previous by thread: Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- Next by thread: Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***
- Index(es):
Relevant Pages
|
Loading