Re: char ** realloc
From: Allin Cottrell (cottrell_at_wfu.edu)
Date: 04/30/03
- Next message: Joshua Jones: "Re: char ** realloc"
- Previous message: Marc Rochkind: "Re: Exclusive file access"
- In reply to: Joshua Jones: "char ** realloc"
- Next in thread: Joshua Jones: "Re: char ** realloc"
- Reply: Joshua Jones: "Re: char ** realloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
- Next message: Joshua Jones: "Re: char ** realloc"
- Previous message: Marc Rochkind: "Re: Exclusive file access"
- In reply to: Joshua Jones: "char ** realloc"
- Next in thread: Joshua Jones: "Re: char ** realloc"
- Reply: Joshua Jones: "Re: char ** realloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|