While I was using delete operator on pointers, I saw something that I could not understand. I think it is best to show it on an example:
#include <iostream>
using namespace std;
int main() {
int* p = new int[5]{3, 4, 5, 6, 7};
cout << p[0] << p[1] << p[2] << p[3] << p[4] << '\n';
delete[] p;
cout << p[0] << p[1] << p[2] << p[3] << p[4] << '\n';
return 0;
}
The result:
34567
00567
After the delete operation why first two elements turned to zeros? Thanks for your attention. I’m looking forward to your replies.