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.
On 3 Ott, 11:43, santosh <santosh....@gm ail.comwrote:
frakie wrote:
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.
On 3 Ott, 11:43, santosh <santosh....@gm ail.comwrote:
>frakie wrote:
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.
>
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.
--
Chris "eg on assignment or parameter passing or ..." Dollin
On Oct 3, 2:38 pm, frakie <frakie...@gmai l.comwrote:
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(initial ized 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.
On Oct 3, 2:38 pm, frakie <frakie...@gmai l.comwrote:
>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(initial ized 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.
--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Oct 3, 2:38 pm, frakie <frakie...@gmai l.comwrote:
>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(initial ized 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.
--
Flash Gordon
>On Oct 3, 2:38 pm, frakie <frakie...@gmai l.comwrote:
>>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(initia lized explicitly or has a random garbage)is pointing to
>freed memory
>
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?
--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
>>frakie <frakie...@gmai l.comwrote:
>>>
>>>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(initi alized explicitly or has a random garbage)is
>>pointing to freed memory
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.
>>On Oct 3, 2:38 pm, frakie <frakie...@gmai l.comwrote:
>>>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(initi alized explicitly or has a random garbage)is pointing to
>>freed memory
>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.
--
Flash Gordon
Comment