On Feb 2, 12:10 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
tedu wrote On 02/02/07 13:28,:
On Jan 25, 4:03 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>In article <1169768146.763494.270...@a75g2000cwd.googlegroups .com>,
>tedu <t...@zeitbombe.orgwrote:
>>>>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.
>>>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.
[little slow noticing the followup.] but one can keep a list of freed
pointers (or conversely, a list of all allocated pointers).
In isgood(p), what if p points to a perfectly valid
object that wasn't obtained from malloc()?
Or, what if p points "into" rather than "at" a block
of allocated memory?
good points. i was focusing too heavily on "can this go to free?"
perhaps. i generally try to avoid too many 'pointers into objects'
running loose, as they frequently end up in unexpected places. so in
that sense, i would count them as bad.
Or, what if p points to a block of memory that has
been freed and then reallocated?
it goes back in the list. true, it won't find stale pointers in that
case.