Pointer: why could a deleted pointer be dereferenced?

B

B. Penn

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;
 
D

David Hilsee

B. Penn said:
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.
 
C

Cy Edmunds

B. Penn said:
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;

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

David Hilsee

David Hilsee said:
"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.
 
D

Daniel T.

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

B. Penn

(e-mail address removed) (B. Penn) wrote in message
Thank you guys for the explanation. It cleared quite a bit.

:)
B. Penn
 
O

Old Wolf

Cy Edmunds said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top