Checking freed pointer...

F

frakie

Hi 'body,
is there a method to check if a pointer is pointing a freed memory
location?
 
S

santosh

frakie said:
Hi 'body,
is there a method to check if a pointer is pointing a freed memory
location?

Non by using Standard facilities. You could, and should, keep track of this
information yourself. How exactly you do so is up to you and the projects
requirements.

One simple method is to set to NULL any pointer that has not been
initialised to point to valid storage, or such storage has been free'ed.
 
F

frakie

Non by using Standard facilities. You could, and should, keep track of this
information yourself. How exactly you do so is up to you and the projects
requirements.

One simple method is to set to NULL any pointer that has not been
initialised to point to valid storage, or such storage has been free'ed.

D'oh!

Thank you..
 
C

Chris Dollin

frakie said:
D'oh!

Thank you..

Note this this method /does not work/ if pointer values get copied around;
setting one copy of a freed pointer to null won't do anything to the other
copies, which now hold non-null non-valid values.

You need a carefully-followed global policy.
 
B

banu

Hi 'body,
is there a method to check if a pointer is pointing a freed memory
location?

I think the question is whether its is possible to test if a
pointer(initialized explicitly or has a random garbage)is pointing to
freed memory i.e memory which is not part of the current process in
execution.
Although I don't know the std library function or code for this, but
work around would be to find out the absolute memory address space for
the program in execution. This would involve finding address range
for code segment (cs), data segment (ds) and stack. Once u have the
address range, u can test the address value stored in pointer against
the address range and take decision accordingly.

Maybe I am not fully correct with the solution, but It seems right to
me at least logically.

Regards,
Varun
 
K

Keith Thompson

banu said:
I think the question is whether its is possible to test if a
pointer(initialized explicitly or has a random garbage)is pointing to
freed memory i.e memory which is not part of the current process in
execution.
Although I don't know the std library function or code for this, but
work around would be to find out the absolute memory address space for
the program in execution. This would involve finding address range
for code segment (cs), data segment (ds) and stack. Once u have the
address range, u can test the address value stored in pointer against
the address range and take decision accordingly.

Maybe I am not fully correct with the solution, but It seems right to
me at least logically.

Please don't use silly abbreviations like "u" for "you".

Standard C has no concept of code segment, data segment, or stack.
Any program that depends on such things is not going to be portable,
and could easily break even between one version of the compiler or OS
and the next.

Checking ranges of addresses, even if it's possible, isn't going to
solve the problem anyway. Just examining the value of a freed pointer
invokes undefined behavior; it's likely to be harmless on most
implementations, but a system *could* check a pointer value for
validity when loading it into a register, and trap if it's invalid.

Even if you can safely examine indeterminate pointer values, the fact
that allocated memory can be re-used causes problems. For example:

some_type *ptr1, *ptr2;

ptr1 = malloc(sizeof *ptr1);
/* ... */
free(ptr1);
/*
* ptr1 now has an indeterminate value
*/

ptr2 = malloc(sizeof *ptr2);
/*
* malloc() could easily re-use the same chunk of memory that was
* used for ptr1. Any test on the value of ptr1 would falsely
* indicate that it points to a valid chunk of memory.
*/

The only real solution is to keep track of it yourself.
 
F

Flash Gordon

banu wrote, On 03/10/07 20:06:
I think the question is whether its is possible to test if a
pointer(initialized explicitly or has a random garbage)is pointing to
freed memory
Yes.

i.e memory which is not part of the current process in
execution.

No, that is a different (but possibly related) question. free does not
necessarily remove the memory from the current process, in fact it is
*normal* for the process to keep ownership of the memory.
Although I don't know the std library function or code for this,

There is not.
but
work around would be to find out the absolute memory address space for
the program in execution.

Which cannot be done in standard C and might not be possible on some
implementations.
This would involve finding address range
for code segment (cs), data segment (ds) and stack.

Not all implementations arrange their memory like that.
Once u have the

Please don't use contractions like "u" for you. They make it harder for
people to read your posts, especially for those for whom English (or
American) is a second language.
address range, u can test the address value stored in pointer against
the address range and take decision accordingly.

That does not answer the question even where it is possible.
Maybe I am not fully correct with the solution, but It seems right to
me at least logically.

Not only is it not "fully correct" it is not even close to being
correct. The only "correct" method is to use whatever the specific
implementation of interest provides. Even then it may not be possible
since on some implementations how malloc works behind the scenes depends
on things completely outside the programmers control.
 
K

Keith Thompson

Flash Gordon said:
banu wrote, On 03/10/07 20:06:

Yes.
[big snip]

Are you saying "Yes, that's the question", or are you saying
that the answer to the question is Yes?

I don't think there's any portable way to determine whether a pointer
points to freed memory. There may not even be a non-portable way.
Are you suggesting there is?
 
C

CBFalconer

Keith said:
Flash Gordon said:
banu wrote, On 03/10/07 20:06:

Yes.
[big snip]

Are you saying "Yes, that's the question", or are you saying
that the answer to the question is Yes?

I don't think there's any portable way to determine whether a
pointer points to freed memory. There may not even be a
non-portable way. Are you suggesting there is?

You can't even tell if a pointer is to malloced memory or something
else. Except by remembering what you did with it.
 
F

Flash Gordon

Keith Thompson wrote, On 04/10/07 01:39:
Flash Gordon said:
banu wrote, On 03/10/07 20:06:
Yes.
[big snip]

Are you saying "Yes, that's the question", or are you saying
that the answer to the question is Yes?

I was saying that yes, that is the question being asked. Perhaps I
should have made that clearer.
I don't think there's any portable way to determine whether a pointer
points to freed memory. There may not even be a non-portable way.
Are you suggesting there is?

I agree that there is no portable way to do this, and I'm fairly sure
that the rest of my post indicated that I don't think there is any
non-portable way to do it on some systems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top