473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer: why could a deleted pointer be dereferenced?

Hi,
I was testing pointers and found that I could still dereference a
pointer and access the value/variable it pointed to after deleting it,
which confused me for "the variable it pointed to is deleted and it
now points to nowhere". Did I do anything wrong? I checked a couple
of references but couldn't explain it. Could anyone kindly clear it
for me please?

Here is my code, which compiled with both GNU C++ and Visual Studio
..Net:

-------------------------------
int *p = new int;
*p = 5;
cout<<p<<' '<<*p<<endl;
delete p; // DELETE
cout<<p<<' '<<*p<<endl; // could be dereferenced
*p = 10; // could access the variable
cout<<p<<' '<<*p<<endl;
-------------------------------

And here is the output of the code:

-------------------------------
00322F80 5
00322F80 -572662307 // why not 0 and the junk value ?
00322F80 10 // how come ???
-------------------------------

Thanks,
B. Penn
Jul 22 '05 #1
6 2880
"B. Penn" <pe**@wayne.edu > wrote in message
news:b3******** *************** **@posting.goog le.com...
Hi,
I was testing pointers and found that I could still dereference a
pointer and access the value/variable it pointed to after deleting it,
which confused me for "the variable it pointed to is deleted and it
now points to nowhere". Did I do anything wrong? I checked a couple
of references but couldn't explain it. Could anyone kindly clear it
for me please?


"Nowhere" might be short for "nowhere that you should be accessing" or
"nowhere you can access without causing undefined behavior." When you
deallocate memory, that does not necessarily mean the pointers that pointed
to the memory now point to something that you can't possibly access. They
may still, in fact, point to some region of memory that you could access
without causing something horrible to happen to your application. However,
the behavior that results from dereferencing the pointer(s) is undefined in
the eyes of the standard, so it's best to either set the pointer(s) to null
or discard them and go about your business. Once you deallocate memory,
just leave it alone.

--
David Hilsee
Jul 22 '05 #2
"B. Penn" <pe**@wayne.edu > wrote in message
news:b3******** *************** **@posting.goog le.com...
Hi,
I was testing pointers and found that I could still dereference a
pointer and access the value/variable it pointed to after deleting it,
which confused me for "the variable it pointed to is deleted and it
now points to nowhere". Did I do anything wrong? I checked a couple
of references but couldn't explain it. Could anyone kindly clear it
for me please?

Here is my code, which compiled with both GNU C++ and Visual Studio
.Net:

-------------------------------
int *p = new int;
*p = 5;
cout<<p<<' '<<*p<<endl;
delete p; // DELETE
cout<<p<<' '<<*p<<endl; // could be dereferenced
*p = 10; // could access the variable
cout<<p<<' '<<*p<<endl;
-------------------------------

And here is the output of the code:

-------------------------------
00322F80 5
00322F80 -572662307 // why not 0 and the junk value ?
00322F80 10 // how come ???
-------------------------------

Thanks,
B. Penn


Because "deleted pointers" is a commonly used misnomer. The pointer is NOT
deleted. The reservation you placed on the memory it points to was
cancelled. The pointer itself is still there; it's just that dereferencing
it now invokes undefined behavior.

In modern C++ design there is less reason to use the new/delete paradigm.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
"David Hilsee" <da************ *@yahoo.com> wrote in message
news:rY******** ************@co mcast.com...
"Nowhere" might be short for "nowhere that you should be accessing" or
"nowhere you can access without causing undefined behavior."


Clarification: I meant through the pointers that were pointing to the region
of memory when the memory was deallocated.

--
David Hilsee
Jul 22 '05 #4
pe**@wayne.edu (B. Penn) wrote:
Hi,
I was testing pointers and found that I could still dereference a
pointer and access the value/variable it pointed to after deleting it,
which confused me for "the variable it pointed to is deleted and it
now points to nowhere". Did I do anything wrong? I checked a couple
of references but couldn't explain it. Could anyone kindly clear it
for me please?


int* p = new int( 5 );
delete p;

at the point after the delete, p doesn't "point to nowhere" rather, it
points to somewhere but the language doesn't define where. Dereferencing
a pointer that doesn't point to a valid location in memory is
'undefined' which means the system is free to do whatever it wants when
you do it. In your example, the system simply returns whatever p used to
point to.
Jul 22 '05 #5
pe**@wayne.edu (B. Penn) wrote in message news:<b3******* *************** ***@posting.goo gle.com>...

Thank you guys for the explanation. It cleared quite a bit.

:)
B. Penn
Jul 22 '05 #6
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote:
"B. Penn" <pe**@wayne.edu > wrote:

I was testing pointers and found that I could still dereference a
pointer and access the value/variable it pointed to after deleting it,
which confused me for "the variable it pointed to is deleted and it
now points to nowhere".


Because "deleted pointers" is a commonly used misnomer. The pointer is NOT
deleted. The reservation you placed on the memory it points to was
cancelled. The pointer itself is still there; it's just that dereferencing
it now invokes undefined behavior.


Accessing its value invokes undefined behaviour too. After being deleted,
the pointer's value is indeterminate. (One reason for this is to support
architectures where the hardware traps if you load an address register
with an address that your process does not have access to).
Jul 22 '05 #7

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

Similar topics

11
1952
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
110
9972
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
3
2364
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
35
2907
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
204
13142
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
5
5107
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
51
4478
by: Kuku | last post by:
What is the difference between a reference and a pointer?
49
2819
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code ;-): char *cp; void *vp; void **vpp;
7
1761
by: shanemh | last post by:
I'm starting out with c++ and for some reason I cant get my brain around this one: If I have the following: void Foo (someClass& x) {} int Main (void) {
0
10604
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
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
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
5536
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
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.