Why does "delete" not delete?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Susman

    Why does "delete" not delete?

    Hi,

    When I call "clean" as shown below, shouldn't I be getting
    segmentation fault due to garbage data? I am compiling with CC and the
    output is 5. Does "delete" not delete in this situation or is it no
    guarantee that it will print 5 or go wrong?

    void clean( int* ptr )
    {
    delete ptr;
    }

    //...

    int* ptr = new int( 5 );

    clean( ptr );

    cout << *ptr << endl;
  • alasham.said@gmail.com

    #2
    Re: Why does &quot;delete&qu ot; not delete?

    On Mar 10, 5:11 pm, "D. Susman" <derya.sus...@g mail.comwrote:
    Hi,
    >
    When I call "clean" as shown below, shouldn't I be getting
    segmentation fault due to garbage data? I am compiling with CC and the
    output is 5. Does "delete" not delete in this situation or is it no
    guarantee that it will print 5 or go wrong?
    >
    void clean( int* ptr )
    {
    delete ptr;
    >
    }
    >
    //...
    >
    int* ptr = new int( 5 );
    >
    clean( ptr );
    >
    cout << *ptr << endl;
    It is 'undefined behaviour', so the result may vary from one
    implementation to another, and on different runs of the same program.

    Regards.

    Comment

    Working...