"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnk6ovy9eo.fsf@nuthaus.mib.org...[color=blue]
> "DHOLLINGSWORTH2" <DHOLLINGSWORTH2@cox.net> writes:[color=green]
>> "G Fernandes" <ge.fernandes@gmail.com> wrote in message
>> news:1109309175.642496.194930@o13g2000cwo.googlegr oups.com...[color=darkred]
>>> Comparisons of pointer variables to 0 are automatically converted to
>>> comparisons to NULL (which can be represented at the bit level but
>>> something non-zero).
>>>
>>> But how about using !ptr or ptr in test conditions (of loops, if or ?:)
>>> ?
>>>
>>> For example:
>>>
>>> char *ptr = malloc(1);
>>> if(!ptr)
>>> {
>>> /*code*/
>>> }
>>>
>>> It works for me, but I happen to know that my implementation uses zeros
>>> to represent NULL. Will this be portable to systems that don't have
>>> zero based NULL?
>>>
>>> Thanking in advance.
>>>[/color]
>> For the most part it will work. However, sometimes the case comes up
>> where
>> ptr != 0 , and ptr is not a valid pointer. Don't assume that all invalid
>> pointers are zero, only that all zeros are null.[/color]
>
> First of all, "if (!ptr)" and "if (ptr != 0)" are exactly equivalent;
> they both test whether the pointer is non-null.
>
> It's guaranteed that malloc() returns either a null pointer or a valid
> pointer (unless memory has been corrupted by something that invoked
> undefined behavior, in which case all bets are off anyway).
>
> You might as well assume that any non-null pointer is valid, because
> there's no way in standard C to determine that a non-null pointer is
> invalid. You just have to make sure that you don't use any invalid
> pointers in the first place.
>
> Section 5 of the C FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
> discusses null pointers; it's highly recommended.
>
> --
> Keith Thompson (The_Other_Keith)
kst-u@mib.org
> <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*>
> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.[/color]
You seem to have forgotten what was written. The use of malloc for "For the
Sake of Example" and not the Subject of his question.
If anything I've said here is incorrect I'll shoot myself!