473,385 Members | 1,676 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,385 software developers and data experts.

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 2849
"B. Penn" <pe**@wayne.edu> wrote in message
news:b3*************************@posting.google.co m...
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.google.co m...
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********************@comcast.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.google.c om>...

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

:)
B. Penn
Jul 22 '05 #6
"Cy Edmunds" <ce******@spamless.rochester.rr.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
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
110
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...
3
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. ...
35
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...
204
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 =...
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
49
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...
7
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
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.