By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Pointer: why could a deleted pointer be dereferenced?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.