Pointer: why could a deleted pointer be dereferenced?

Discussion in 'C++' started by B. Penn, Aug 5, 2004.

  1. B. Penn

    B. Penn Guest

    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
    B. Penn, Aug 5, 2004
    #1
    1. Advertising

  2. B. Penn

    David Hilsee Guest

    "B. Penn" <> wrote in message
    news:...
    > 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
    David Hilsee, Aug 5, 2004
    #2
    1. Advertising

  3. B. Penn

    Cy Edmunds Guest

    "B. Penn" <> wrote in message
    news:...
    > 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/
    Cy Edmunds, Aug 5, 2004
    #3
  4. B. Penn

    David Hilsee Guest

    "David Hilsee" <> wrote in message
    news:...
    > "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
    David Hilsee, Aug 6, 2004
    #4
  5. B. Penn

    Daniel T. Guest

    (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.
    Daniel T., Aug 6, 2004
    #5
  6. B. Penn

    B. Penn Guest

    (B. Penn) wrote in message news:<>...

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

    :)
    B. Penn
    B. Penn, Aug 9, 2004
    #6
  7. B. Penn

    Old Wolf Guest

    "Cy Edmunds" <> wrote:
    > "B. Penn" <> 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).
    Old Wolf, Aug 9, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matt
    Replies:
    10
    Views:
    3,310
    Grant Wagner
    Sep 2, 2005
  2. haig
    Replies:
    15
    Views:
    27,036
    java_programmer
    Jan 11, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    7
    Views:
    940
    Bo Persson
    Oct 30, 2006
  5. Chumley the Walrus
    Replies:
    2
    Views:
    239
    Tom Gosselin
    Aug 10, 2004
Loading...

Share This Page