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?