473,769 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8339
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....@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..

Oct 3 '07 #3
frakie wrote:
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

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...@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.

Regards,
Varun

Oct 3 '07 #5
banu <va***********@ gmail.comwrites :
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) 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...@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
Oct 3 '07 #7
Flash Gordon <sp**@flash-gordon.me.ukwri tes:
banu wrote, On 03/10/07 20:06:
>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) 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.ukwri tes:
>banu wrote, On 03/10/07 20:06:
>>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.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.schneier.co m/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

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

Similar topics

72
3630
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
5190
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
51
4237
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 some instances where i did just that and printf reported something like : (null). I'm asking because i read somewhere that it is not valid. I thought it simply wasn't valid to dereference a pointer who is _not_ set to point to anything.
125
6605
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 problem. http://www.jilp.org/vol9/v9paper10.pdf Specifically, they measured the overhead of a bounds
0
9586
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.