473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Checking freed pointer...

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

Oct 3 '07 #1
10 8238
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.

Oct 3 '07 #2
On 3 Ott, 11:43, santosh <santosh....@gmail.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..

Oct 3 '07 #3
frakie wrote:
On 3 Ott, 11:43, santosh <santosh....@gmail.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

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Oct 3 '07 #4
On Oct 3, 2:38 pm, frakie <frakie...@gmail.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(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

Oct 3 '07 #5
banu <va***********@gmail.comwrites:
On Oct 3, 2:38 pm, frakie <frakie...@gmail.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(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.

--
Keith Thompson (The_Other_Keith) ks***@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"
Oct 3 '07 #6
banu wrote, On 03/10/07 20:06:
On Oct 3, 2:38 pm, frakie <frakie...@gmail.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(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.
--
Flash Gordon
Oct 3 '07 #7
Flash Gordon <sp**@flash-gordon.me.ukwrites:
banu wrote, On 03/10/07 20:06:
>On Oct 3, 2:38 pm, frakie <frakie...@gmail.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(initialized 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_Keith) ks***@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"
Oct 4 '07 #8
frakie wrote:
>
is there a method to check if a pointer is pointing a freed memory
location?
No.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 4 '07 #9
Keith Thompson wrote:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>banu wrote, On 03/10/07 20:06:
>>frakie <frakie...@gmail.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(initialized 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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.schneier.com/crypto-gram-0702.html#8>
<http://www.aaxnet.com/editor/edit043.html>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 4 '07 #10
Keith Thompson wrote, On 04/10/07 01:39:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>banu wrote, On 03/10/07 20:06:
>>On Oct 3, 2:38 pm, frakie <frakie...@gmail.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(initialized 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
Oct 4 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: ravi | last post by:
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?
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.