Thread: after delete ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    7

    after delete ...

    code like:

    Code:
    int* e = new int;
    
    delete e;
    
    *e = 3; // why no error?
    
    cout << *e << endl; //why no error?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The compiler has no way of knowing that e is no longer a valid pointer and so dosen't generate a compiler error. The memory pointed to by e is possibly unchanged because nothing else has used it yet howerver this memory is not marked as allocated and so could be reused at any time which would mean you are overwriting some memory.
    After delete you should set the pointer to a NULL pointer to prevent these errors.
    Last edited by Quantum1024; 11-21-2005 at 07:12 AM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This is part of the reason why pointers are called unsafe: they just don't know that they're invalid and might or might not fail. But don't forget one of the many Laws of Computer Science (I think Bjarne Stroustrup first formulated this one): "Undefined behaviour means it works while writing it, works while testing it, and crashes for your most important customer."
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I thought it was "blows up".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Quantum1024
    After delete you should set the pointer to a NULL pointer to prevent these errors.
    That doesn't work.

    Code:
    x = new int;
    *x = 5;
    y = x;
    
    delete x;
    x = NULL;
    
    *y = 7;
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    because y conains the original value of x. when you change x to NULL that doesn't change the value of y -- the value of y remains unchanged. y is NOT a pointer to x but a copy of x.

    try this -- note the double asterisks, in this example y is a pointer to x, so when you change x you will also change the value to which y points.
    Code:
    x = new int;
    *x = 5;
    int **y = &x;
    
    delete x;
    x = NULL;
    
    **y = 7;
    Last edited by Ancient Dragon; 11-21-2005 at 12:33 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Ancient Dragon
    Well duuuh! is ain't suppose to work!
    I like that one.

    And "proper feedback"? Well, one of my compilers prints "NULL pointer assignment" but another one lets it pass.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's about runtime feedback.

    Compare:
    Code:
    int *p = new int;
    *p = 5;
    delete p;
    *p = 6;
    -> will probably go undetected.

    Code:
    int *p = new int;
    *p = 5;
    delete p;
    p = 0;
    *p = 6;
    -> will crash.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM