Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to know the memory pointed by a ptr is freed? (http://www.velocityreviews.com/forums/t434913-how-to-know-the-memory-pointed-by-a-ptr-is-freed.html)

ravi 08-17-2004 09:29 AM

How to know the memory pointed by a ptr is freed?
 
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?


Jens.Toerring@physik.fu-berlin.de 08-17-2004 10:15 AM

Re: How to know the memory pointed by a ptr is freed?
 
ravi <ec_au_ravi2000@yahoo.com> 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 ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Ravi Uday 08-17-2004 10:28 AM

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

"ravi" <ec_au_ravi2000@yahoo.com> wrote in message
news:cfsj55$9ah@odak26.prod.google.com...
> 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



Keith Thompson 08-17-2004 05:41 PM

Re: How to know the memory pointed by a ptr is freed?
 
"Ravi Uday" <raviuday@gmail.com> writes:
> "ravi" <ec_au_ravi2000@yahoo.com> wrote in message
> news:cfsj55$9ah@odak26.prod.google.com...
> > 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) 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.

Kenny McCormack 08-17-2004 06:39 PM

Re: How to know the memory pointed by a ptr is freed?
 
In article <lnwtzxu0de.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:
>"Ravi Uday" <raviuday@gmail.com> writes:
>> "ravi" <ec_au_ravi2000@yahoo.com> wrote in message
>> news:cfsj55$9ah@odak26.prod.google.com...
>> > 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.


Malcolm 08-17-2004 07:17 PM

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

"Kenny McCormack" <gazelle@yin.interaccess.com> 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)
..



Keith Thompson 08-17-2004 07:25 PM

Re: How to know the memory pointed by a ptr is freed?
 
gazelle@yin.interaccess.com (Kenny McCormack) writes:
> In article <lnwtzxu0de.fsf@nuthaus.mib.org>,
> Keith Thompson <kst-u@mib.org> wrote:
> >"Ravi Uday" <raviuday@gmail.com> writes:
> >> "ravi" <ec_au_ravi2000@yahoo.com> wrote in message
> >> news:cfsj55$9ah@odak26.prod.google.com...
> >> > 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) 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.

Keith Thompson 08-17-2004 08:29 PM

Re: How to know the memory pointed by a ptr is freed?
 
"Malcolm" <malcolm@55bank.freeserve.co.uk> 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) 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.

Default User 08-17-2004 08:44 PM

Re: How to know the memory pointed by a ptr is freed?
 
Kenny McCormack wrote:
>
> In article <lnwtzxu0de.fsf@nuthaus.mib.org>,
> Keith Thompson <kst-u@mib.org> wrote:
> >"Ravi Uday" <raviuday@gmail.com> 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

CBFalconer 08-17-2004 10:05 PM

Re: How to know the memory pointed by a ptr is freed?
 
Malcolm wrote:
> "Kenny McCormack" <gazelle@yin.interaccess.com> 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




All times are GMT. The time now is 10:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.