Re: checking if pointer is NULL allowed?
Richard Tobin wrote:
We check it for null, which is a valid enough check for a debug/logging
routine. If it has been freed and causes the debug to crash, then we
can consider this a "free" assert and can fix it, because we are
generally not interested in the pointer once it has been freed.
That is, in the context of a debug statement, a "valid pointer" is one
that is either null or can be dereferenced legally to pass to printf.
Richard Tobin wrote:
In article <1169768146.763 494.270880@a75g 2000cwd.googleg roups.com>,
tedu <tu@zeitbombe.o rgwrote:
>
>
>
>
You can tell whether it's null (which I suspect is what was meant) but
you can't check that it's "valid to dereference" - it might be a
free()ed pointer for example.
>
tedu <tu@zeitbombe.o rgwrote:
>
>>>It is common practice in many shops I've worked at to have a function or
>>>macro used in debugging routines that can take a pointer and return the
>>>pointer or "null", depending on if the pointer is valid to dereference
>>>or not.
>>>macro used in debugging routines that can take a pointer and return the
>>>pointer or "null", depending on if the pointer is valid to dereference
>>>or not.
>>Maybe, but that's not possible in standard C.
>why not?
You can tell whether it's null (which I suspect is what was meant) but
you can't check that it's "valid to dereference" - it might be a
free()ed pointer for example.
>
routine. If it has been freed and causes the debug to crash, then we
can consider this a "free" assert and can fix it, because we are
generally not interested in the pointer once it has been freed.
That is, in the context of a debug statement, a "valid pointer" is one
that is either null or can be dereferenced legally to pass to printf.
Comment