0

I am new to C++, have trying hands with it. Today i was going through dynamic memory allocation. According to following link:

http://msdn.microsoft.com/en-us/library/h6227113.aspx

"Using the delete operator on an object deallocates its memory. A program that dereferences a pointer after the object is deleted can have unpredictable results or crash."

But in following example, even after using delete, when i try to print the contain of that location it prints the correct value? How?

I am really confused ....please guide me? Thank you.

     int main()
 {
  int max_Value = 12;
     int *arr = new int [max_Value];
     arr[4] = 9;
     cout<< "arr of four is having: "<<arr[4]<<endl;

     delete[]arr;

    int i = 0;
    for(i=0; i<12; i++)
         cout<< arr[i]<<endl;
 return 0;
 }
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
SNJ2000
  • 29
  • 4

3 Answers3

1

When you delete (or delete[]) a pointer, you basicaly tells the underlying system that you will not use this memory bloc anymore. The system now owns the bloc, and may use it (for instance: to alloc something else). In your case, there is no work done after the delete, so the memory bloc released is still untouched. You can't rely on it though, it's a particular case. You could try to make other newallocations, fill it with data and observe your old pointee data, you may observe that the memory bloc were affected by other writes operations.

Here is my result, YMMV as we deal explicitly with undefined behaviours:

int main() {
    int max_Value = 12;
    int *arr = new int[max_Value];
    arr[4] = 9;
    cout << "arr of four is having: " << arr[4] << endl;

    delete[] arr;

    // print old data
    for (int i = 0; i < 12; i++)
        cout << arr[i] << " ";
    cout << endl;

    // another write (overwrite?) operation
    int *arr2 = new int[max_Value];
    for (int i = 0; i < 12; i++)
        arr2[i] = -1;

    // print again old data
    for (int i = 0; i < 12; i++)
        cout << arr[i] << " ";
    return 0;
}

output

arr of four is having: 9
3619976 3615400 0 0 9 62374 3604676 3611152 0 0 0 0
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 

The deleted data is given back tou you by the os !

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
johan d
  • 2,798
  • 18
  • 26
0

When you delete an object in C++ its memory doesn't go away - it's simply flagged as unused. In the same way that you can sometimes recover parts or all of a file after deleting it from a hard drive, future reads of a deleted object address may contain remnants of the data that was there previously. You should never do this intentionally, however, since this kind of access can cause a segfault and crash your program.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43
0

It not crashes because you compiled with Runtime Error Checks disabled. You can enable them using /RTC compilation switch.

MSDN talks about unpredictable behavior because it's indeed unpredictable: next time you will try to allocate dynamic memory, there is a chance to get (partly) the same area, you recently deallocated. This will cause your program to use the same memory area in two (or more) places, while assuming that only one accessing it. What will be the result? Unpredictable - depends on your code.

Denis Itskovich
  • 4,383
  • 3
  • 32
  • 53