question regarding delete functionality.

[b][red]This message was edited by akalexi312 at 2006-9-29 20:51:53[/red][/b][hr]
Hi all i am just trying to test new and delete operators functionality in C++ using gnu g++ and intel icc c++ compilers.The following code allocates a 12 integer array and deletes it.Even after deleting i am still able to access the array elements.Can some one exaplain why ?

code:
=========
#include
#include

using namespace std;

int main(int argc,char *argv[]) {

int *pint;

try {
pint = new int[12];
} catch(bad_alloc xa) {
std::cout << "failure" << std::endl;
exit(1);
}

for(int i=0;i<12;i++)
pint[i]=i;

delete [] pint;

for(int i=0;i<12;i++)
std::cout << pint[i] << std::endl;

}

output:
=============
0
1
2
3
4
5
6
7
8
9
10
11

I think there is issue in how delete is being called/used here ?
Would be thankful for replies.


Comments

  • [b][red]This message was edited by gautam at 2006-9-29 22:58:21[/red][/b][hr]
    [b][red]This message was edited by gautam at 2006-9-29 22:57:53[/red][/b][hr]
    No - The delete function merely says that those blocks that were allocated by new can now be reused when new is called in again. You must explicitly set the variable to NULL.

    Look into the implementation of malloc and free function as well around on the net to understant what is going on.

    However the delete function does call a class destructor.


    : [b][red]This message was edited by akalexi312 at 2006-9-29 20:51:53[/red][/b][hr]
    : Hi all i am just trying to test new and delete operators functionality in C++ using gnu g++ and intel icc c++ compilers.The following code allocates a 12 integer array and deletes it.Even after deleting i am still able to access the array elements.Can some one exaplain why ?
    :
    : code:
    : =========
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main(int argc,char *argv[]) {
    :
    : int *pint;
    :
    : try {
    : pint = new int[12];
    : } catch(bad_alloc xa) {
    : std::cout << "failure" << std::endl;
    : exit(1);
    : }
    :
    : for(int i=0;i<12;i++)
    : pint[i]=i;
    :
    : delete [] pint;
    :
    : for(int i=0;i<12;i++)
    : std::cout << pint[i] << std::endl;
    :
    : }
    :
    : output:
    : =============
    : 0
    : 1
    : 2
    : 3
    : 4
    : 5
    : 6
    : 7
    : 8
    : 9
    : 10
    : 11
    :
    : I think there is issue in how delete is being called/used here ?
    : Would be thankful for replies.
    :
    :
    :





  • [blue]Even if you think you can access the data after delete - you should not do it. The C++ standard specifies that lifetime of a memory block allocated with new continues until delete is called. After delete - the accessibility and values from that memory block is compiler dependant. For example, Visual C++ (in DEBUG mode) clears the memory into 0xCC (or other 'bad' value) bytes when deleted. If you run your code in that compiler - you will not see your values anymore in DEBUG mode.[/blue]
  • : [blue]Even if you think you can access the data after delete - you should not do it. The C++ standard specifies that lifetime of a memory block allocated with new continues until delete is called. After delete - the accessibility and values from that memory block is compiler dependant. For example, Visual C++ (in DEBUG mode) clears the memory into 0xCC (or other 'bad' value) bytes when deleted. If you run your code in that compiler - you will not see your values anymore in DEBUG mode.[/blue]
    :


    Here is some code to visualize what's been said.
    When "somethingElse" has been allocated, the old values will be lost on most compilers (tried with Borland and gcc).

    [code]
    #include
    #include

    using namespace std;

    int main()
    {
    int* ptr;

    ptr = new int[10];

    cout << "Before delete" << endl;
    for(int i=0; i<10; i++)
    {
    ptr[i] = i;
    cout << ptr[i] << endl;
    }

    delete [] ptr;

    cout << endl << "After delete" << endl;
    for(int i=0; i<10; i++)
    {
    cout << ptr[i] << endl;
    }


    int* somethingElse = new int[10];
    for(int i=0; i<10; i++)
    somethingElse[i] = 10;

    cout << endl << "After next allocation" << endl;
    for(int i=0; i<10; i++)
    {
    cout << ptr[i] << endl;
    }


    delete [] somethingElse;

    return 0;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories