Re: Detecting freed memory
- From: gordonb.n4bk8@xxxxxxxxxxx (Gordon Burditt)
- Date: Thu, 01 Sep 2005 17:09:46 -0000
>int *p;
>
>p = malloc(1);
>
>free(p);
p; /* possible smegmentation fault here */
>
>/* */
>
>
>In the above code, how do I detect that variable p points to nothing?
You can't even TRY: doing anything with the value of p invokes the
wrath of undefined behavior. You might try setting p to NULL
immediately after the free() call, but *DO NOT* assume that the
fact that it's not null means it points somewhere valid unless you
are sure you've coded it that way. Why? Code like:
destructor(p); /* free p and its associated buffers */
cannot set p to NULL in the calling function, assuming destructor()
is an actual function. More often, a function cannot set all of
the copies of the pointer it's freeing to NULL because it doesn't
now where the copies are.
>In other words, how do I detect a freed memory pointer, as opposed to a
>pointer that points to allocated memory?
Keep track of it yourself.
Gordon L. Burditt
.
- References:
- Detecting freed memory
- From: Rob
- Detecting freed memory
- Prev by Date: Re: while (1) vs. for ( ;; )
- Next by Date: Re: Alignment in structure
- Previous by thread: Re: Detecting freed memory
- Next by thread: register allocation
- Index(es):
Relevant Pages
|
|