x = new int(8);
cout << *x << endl;
delete x;
auto y = new int(7);
cout << *x << endl;
*x = 3;
cout << *x << endl;
cout << *y << endl;
delete y;
}
Produce the following output:
8
7
3
3
On almost every system?
This is undefined behavior. Almost every system, x only deleted or marked as not used by program anymore but address is still valid on program space.
This is UB.
swapping that delete/new in lines 4/5 causes different (just as much) undefined behavior
run that -D or other optimization/debug configurations. You will see a lot of fun
Обсуждают сегодня