-2

I am currently using C++ on VScode with C/C++ and CodeRunner application. For some reason, even though adding the delete[]p in my code, the values in the following array remain there. Can someone please help?

#include <iostream>
using namespace std;

int main()
{
    int *p;
    p = new int[2];
    p[0] = 10;
    p[1] = 20;
    delete[]p;
    cout<< "Value in P[0] before "<<p[0]<<endl;
    cout<< "Value in P[1] before "<<p[1]<<endl;

    return 0; 
}

Output:

Value in P[0] before 10
Value in P[1] before 20

Shouldn't the value of P[0] and P[1] be different after we use the delete[]p constructor, as we are deallocating the memory from the heap?

Am I missing something in my C++ program to be able to run this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    Accessing elements in an array after it's been deleted is **undefined behavior**. – selbie Jun 28 '22 at 02:20
  • 1
    Because the C++ standard neither requires, nor forbids, the data in the array to be overwritten. By dereferencing `p` (i.e. accessing `p[0]` and `p[1]`) your program has undefined behaviour, so any outcome is permitted and an implementation (e.g. your compiler and library) can do anything it likes (e.g. retrieve values array that were there before, or reformat your hard drive) and still be correct. If you want memory overwritten, then overwrite that memory BEFORE doing `delete [] p` (some implementations might overwrite it again, but some won't) and don't access it after doing `delete [] p`. – Peter Jun 28 '22 at 02:23
  • 4
    But the most likely explanation is that the in-process memory manager of the C/C++ runtime has claimed the memory back, but hasn't reallocated it before your subsequent `cout` statements . Hence, the memory address still exists and has the same contents. But that behavior could change based on any number of conditions. Often in debug builds, the compiler will insert some special bytes to make it easier to recognize deleted memory being accessed. But that's not standard either. – selbie Jun 28 '22 at 02:23
  • [Here is a nice answer for a similar, but different situation](https://stackoverflow.com/a/6445794/631266) The details are different, but the analogy works just as well here. – Avi Berger Jun 28 '22 at 02:33
  • Accessing contents of a deleted objects [makes demons fly out of your nose](http://www.catb.org/jargon/html/N/nasal-demons.html). These are very nasty, very evil demons. They can make you believe that the data is still there. But it's not. If you don't want to deal with nasal demons, don't ever access anything that's been deleted. – Sam Varshavchik Jun 28 '22 at 02:38
  • 1
    @SamVarshavchik Demons are only created by some systems. Most computers don't have the hardware, firmware, or wetware support. – Peter Jun 28 '22 at 02:46

1 Answers1

2

The behavior of your program is undefined as you're using the expressions p[0] and p[1] after using delete[]p;.

Undefined behavior means anything can happen, don't rely on the output of a program that has UB.

Kal
  • 475
  • 1
  • 16