Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to know the memory pointed by a ptr is freed?

Reply
Thread Tools

How to know the memory pointed by a ptr is freed?

 
 
ravi
Guest
Posts: n/a
 
      08-17-2004
I have a situation where i want to free the memory pointed by a
pointer, only if it is not freed already. Is there a way to know
whether the memory is freed or not?

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-17-2004
ravi <(E-Mail Removed)> wrote:
> I have a situation where i want to free the memory pointed by a
> pointer, only if it is not freed already. Is there a way to know
> whether the memory is freed or not?


No. You got to store this bit of informatioon. The simplest method
is to set all pointers that pointed to memory you free()ed to NULL.
Than you have no chance to free() that memory region twice.

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Ravi Uday
Guest
Posts: n/a
 
      08-17-2004

"ravi" <(E-Mail Removed)> wrote in message
news:cfsj55$(E-Mail Removed)...
> I have a situation where i want to free the memory pointed by a
> pointer, only if it is not freed already. Is there a way to know
> whether the memory is freed or not?
>


Its generally OS/system specific.
Generally there will be some 'poison pattern' written to the pointer if it
has been freed.
So the pattern might be of some clue.

Otherwise AFAIK there are no other ways of finding out whether a pointer is
freed apriorly..

- Ravi


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-17-2004
"Ravi Uday" <(E-Mail Removed)> writes:
> "ravi" <(E-Mail Removed)> wrote in message
> news:cfsj55$(E-Mail Removed)...
> > I have a situation where i want to free the memory pointed by a
> > pointer, only if it is not freed already. Is there a way to know
> > whether the memory is freed or not?

>
> Its generally OS/system specific.
> Generally there will be some 'poison pattern' written to the pointer if it
> has been freed.
> So the pattern might be of some clue.


No, the pointer is typically left alone. The value becomes invalid,
but the bit pattern typically (arguably always) is left unchanged.

Given:

char *ptr = malloc(42);
...
free(ptr);

note that ptr is passed to the free() function by value. free() has
no access to the variable ptr, and can't modify it.

In fact, a subsequent call to malloc() is likely to reuse the same
address. If this happens, ptr will appear to be valid, and will
appear to point to the newly allocated memory -- but if you then call
free(ptr), you're going to clobber memory that was actually allocated
for something else.

The only way to know whether a pointer has been free()d is to keep
track of it yourself.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      08-17-2004
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:
>"Ravi Uday" <(E-Mail Removed)> writes:
>> "ravi" <(E-Mail Removed)> wrote in message
>> news:cfsj55$(E-Mail Removed)...
>> > I have a situation where i want to free the memory pointed by a
>> > pointer, only if it is not freed already. Is there a way to know
>> > whether the memory is freed or not?

>>
>> Its generally OS/system specific.
>> Generally there will be some 'poison pattern' written to the pointer if it
>> has been freed.
>> So the pattern might be of some clue.

>
>No, the pointer is typically left alone. The value becomes invalid,
>but the bit pattern typically (arguably always) is left unchanged.
>
>Given:
>
> char *ptr = malloc(42);
> ...
> free(ptr);
>
>note that ptr is passed to the free() function by value. free() has
>no access to the variable ptr, and can't modify it.


The implication was that the memory area pointed to by "ptr" would be
scrambled in some implementation-defined way - not that ptr would itself be
modified. The point is that if this were done, then the programmer could,
in an implementation-specific way, tell if the memory was valid.

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      08-17-2004

"Kenny McCormack" <(E-Mail Removed)> wrote
[ freed pointers ]
> The implication was that the memory area pointed to by "ptr" would be
> scrambled in some implementation-defined way - not that ptr would itself

be
> modified. The point is that if this were done, then the programmer could,
> in an implementation-specific way, tell if the memory was valid.
>

The implemetation is allowed to modify a pointer passed to free(). Generally
this isn't done because it is simpler to implement free() as a normal
function.
Some platforms do chew garbage, but usually not in performance-critical
mode. Anyway, this is a bad way to test a pointer. Firstly the garbage value
might be used for real data, and secondly and more importantly,
dereferencing a freed pointer causes undefined behaviour, so your program
would be incorrect (even if the technique happens to work on that platform)
..


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-17-2004
(E-Mail Removed) (Kenny McCormack) writes:
> In article <(E-Mail Removed)>,
> Keith Thompson <(E-Mail Removed)> wrote:
> >"Ravi Uday" <(E-Mail Removed)> writes:
> >> "ravi" <(E-Mail Removed)> wrote in message
> >> news:cfsj55$(E-Mail Removed)...
> >> > I have a situation where i want to free the memory pointed by a
> >> > pointer, only if it is not freed already. Is there a way to know
> >> > whether the memory is freed or not?
> >>
> >> Its generally OS/system specific.
> >> Generally there will be some 'poison pattern' written to the pointer if it
> >> has been freed.
> >> So the pattern might be of some clue.

> >
> >No, the pointer is typically left alone. The value becomes invalid,
> >but the bit pattern typically (arguably always) is left unchanged.
> >
> >Given:
> >
> > char *ptr = malloc(42);
> > ...
> > free(ptr);
> >
> >note that ptr is passed to the free() function by value. free() has
> >no access to the variable ptr, and can't modify it.

>
> The implication was that the memory area pointed to by "ptr" would be
> scrambled in some implementation-defined way - not that ptr would itself be
> modified. The point is that if this were done, then the programmer could,
> in an implementation-specific way, tell if the memory was valid.


Hmm. I assumed that

Generally there will be some 'poison pattern' written to the
pointer if it has been freed.

referred to writing to the pointer itself, not writing through it.

But even if that's the case, you could still have the following:

char *ptr;
char *another_ptr;
ptr = malloc(42);
free(ptr); /* ptr is now invalid */
another_ptr = malloc(42); /* might re-use the same memory */

After the second malloc() call, it's likely (but by no means certain)
that ptr will still have the same value, and that it will point to the
chunk of memory allocated by the second call. In language terms, it's
invalid, but there's likely to be no way to detect that.

Bottom line: you just have to keep track of this stuff yourself.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-17-2004
"Malcolm" <(E-Mail Removed)> writes:
[...]
> The implemetation is allowed to modify a pointer passed to
> free(). Generally this isn't done because it is simpler to implement
> free() as a normal function.


This was discussed at length some time ago. I don't think there was
any consensus that an implementation is allowed to do this. It would
have to involve some kind of compiler "magic", since the argument to
free() can be any pointer expression, not necessarily a reference to a
pointer object.

Note that if an implementation does choose to modify a pointer passed
to free(), this could be visible to a strictly conforming program. It
could copy a pointer object's representation to an array of unsigned
char, call free(), copy the post-free() representation to another
array of unsigned char, and compare the arrays. If free() is required
to act like a function (in the sense that it can't modify its
argument), the arrays must appear equal.

In any case, this isn't particularly relevant for C programmers. Most
(all?) existing implementations leave the (now invalid) value in the
pointer object, and a program can't legitimately look at the pointer
value after the call to free() (other than by using the unsigned char
trick, but there's no good reason to do that).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-17-2004
Kenny McCormack wrote:
>
> In article <(E-Mail Removed)>,
> Keith Thompson <(E-Mail Removed)> wrote:
> >"Ravi Uday" <(E-Mail Removed)> writes:


> >> Generally there will be some 'poison pattern' written to the pointer if it
> >> has been freed.
> >> So the pattern might be of some clue.


> >note that ptr is passed to the free() function by value. free() has
> >no access to the variable ptr, and can't modify it.

>
> The implication was that the memory area pointed to by "ptr" would be
> scrambled in some implementation-defined way - not that ptr would itself be
> modified. The point is that if this were done, then the programmer could,
> in an implementation-specific way, tell if the memory was valid.


It's still going to be Undefined Behavior to dereference that pointer.
Not to mention that memory that has been released with a call to free()
may well be reassigned via another call to *alloc(), so relying on some
pattern in the memory to tell whether is valid or not is a fool's
gambit.

If you really are incapable of keeping track, use a memory pool manager.




Brian Rodenborn
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-17-2004
Malcolm wrote:
> "Kenny McCormack" <(E-Mail Removed)> wrote
>
> [ freed pointers ]
>> The implication was that the memory area pointed to by "ptr"
>> would be scrambled in some implementation-defined way - not that
>> ptr would itself be modified. The point is that if this were
>> done, then the programmer could, in an implementation-specific
>> way, tell if the memory was valid.

>
> The implemetation is allowed to modify a pointer passed to free().
> Generally this isn't done because it is simpler to implement
> free() as a normal function.


No it isn't. free receives a pointer by value, and has no idea
where the original value was stored.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Need to know the size of the memory block pointed to by a char* Frodo Baggins C Programming 12 01-03-2007 12:19 PM
const ptr to const ptr ? franco ziade C Programming 3 02-17-2005 04:30 AM
How to convert a double **ptr in a double const ** const ptr? Heiko Vogel C Programming 3 09-14-2004 10:23 AM
what's the difference between delete ptr and ptr=0 -dont they accomplish the same Sid C++ 5 07-29-2004 03:42 AM



Advertisments