Re: char ** realloc

From: Allin Cottrell (cottrell_at_wfu.edu)
Date: 04/30/03


Date: Tue, 29 Apr 2003 23:21:58 -0400

Joshua Jones wrote:
> I've looked at this for a while, and I just can't figure it out.
> The following code is causing my program to crash. Does anyone
> see anything wrong with this code?
>
> -------------
> char ** result;
> ...

What happens in here? "result" is uninitialized at this point,
and hence _not_ a suitable candidate for realloc'ing. If you
set it to NULL (or do an initial malloc) you will be OK.
>
> result = realloc (result, (((*count)+1) *
> sizeof (char *)));

BTW, why all the parentheses? Isn't

result = realloc (result, (*count + 1) * sizeof (char *));

clearer? But actually

result = realloc (result, (*count + 1) * sizeof *result);

is more idiomatic.

-- 
Allin Cottrell
Department of Economics
Wake Forest University, NC


Relevant Pages

  • Re: char ** realloc
    ... Joshua Jones wrote: ... > The following code is causing my program to crash. ... Does anyone have any clues? ...
    (comp.unix.programmer)
  • Re: char ** realloc
    ... Joshua Jones wrote: ... > The following code is causing my program to crash. ... fragment. ... if realloc fails you have lost the original pointer, ...
    (comp.unix.programmer)
  • Re: char ** realloc
    ... In article, Joshua Jones wrote: ... > The following code is causing my program to crash. ... from malloc before you pass it to realloc. ... Neil Cerutti ...
    (comp.unix.programmer)
  • Re: char ** realloc
    ... Joshua Jones wrote: ... >The following code is causing my program to crash. ... What is the value of result the first time this code is executed? ...
    (comp.unix.programmer)