Re: *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***



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
.



Relevant Pages

  • Re: replacing unsigned char * with CString
    ... It means "allocate memory"! ... you should forget the datatype 'char' exists at all in modern programming; ... Basically the reason I like CString so much is that I have needed to have ...
    (microsoft.public.vc.mfc)
  • Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
    ... >ok I did get it to work, the problem was that I set the char* to NULL, ... and you'll be scribbling over random memory. ... If it were intended to allocate ... >memory think when the pointer was set to the what recv was ...
    (comp.unix.programmer)
  • Re: [PATCH 10 of 20] ipath - support for userspace apps using core driver
    ... The memory that we allocate with dma_alloc_coherent is indeed being ... char device's release method is called. ... so I conclude that the struct pages are leaking. ...
    (Linux-Kernel)
  • Re: Memory Access Error
    ... First, scanfexpects a char pointer ... of 'inputstring') you still would have the problem that 'inputstring' ... you a char pointer but not also some memory it would be pointing to. ...
    (comp.os.linux.development.apps)
  • Re: printf
    ... >>> portion of memory. ... char *p; ... is that string literals are not writable, ... >>> You still have to allocate p. ...
    (comp.lang.c)

Loading