realloc

Guy Harris guy at auspex.auspex.com
Tue Apr 4 04:30:23 AEST 1989


>>I read the man page for realloc and it said nothing about this.  Is it
>>not possible for you to type
>>
>>	if (ptr == NULL)
>>		ptr = malloc (nbytes);
>>	else
>>		ptr = realloc (ptr, nbytes);
>>
>>or are you one of those people that assumes (*((char *)NULL) == 0) too?
>[ more flame deleted ]
>
>Pardon my ignorance, but if I'm wrong you can flame me too...
>Does the draft not specify that whatever implementation of NULL is used, 
>the compiler must guarantee that a ptr containing that implementation's
>version of NULL must test as false?

Huh?  What does that have to do with any of this?  The draft does
specify that

	if (x)

is equivalent to

	if (x != 0)

and that, at least for "x" of pointer type, that is in turn equivalent
to

	if (x != NULL)

but in no way does the draft require that

	*((char *)NULL) == 0

and a damn good thing that is - some implementations cause programs that
illegally attempt to dereference the null pointer to blow up, so that
bugs of that sort are caught....

If *you* want to write

	if (p)

rather than

	if (p != NULL)

go ahead; both are equally legal C, and while the "!= NULL" may be
redundant in some sense, at least some of us find that

	if (p != NULL)

easier to read than

	if (p)



More information about the Comp.lang.c mailing list