What is the issue with this code? It gives the output as expected, but there is a runtime error, and I don't know what it is.
Can somebody explain the concept behind it?
int main()
{
int *a = new int(7);//assume the heap memory has address 4F
int *p;
p = a;
cout << a << endl;
cout << p << endl;
cout << *a << endl;
cout << *p << endl;
*p = 10;
cout << *a << endl;
delete p;
delete a;
return 0;
}
pis equal toa, which contains the result ofnew int(7). Doingdelete pfollowed bydelete atherefore releases the same dynamically allocatedinttwice . That causes undefined behaviour.