473,287 Members | 1,960 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,287 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 2844
"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) {
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.