0
Why it is printing the value 578 again in line 8 even after deallocation?
#include <iostream> int main() { int *a=new int; *a=578; std::cout<<*a<<std::endl; delete a; std::cout<<*a; return 5; }
9 Respuestas
+ 2
Yes Ujjawal Gupta
+ 6
delete just marks the address of the resource as available again so that it can be reused.
You cannot rely on what the value might be after it, because using a deleted value is undefined behaviour.
+ 4
Ujjawal Gupta I'm not getting the same thing with your code so I'm confused
https://code.sololearn.com/czL2iyA1Rx7a/?ref=app
+ 2
Ujjawal Gupta but you do see that here you are getting the correct answer - true ?!
+ 1
BroFar am using clang version 9.0.1 to compile c++17 and getting 578 two times
+ 1
Here you are using g++ 10 Ubuntu Ujjawal Gupta on vs code editor.
https://code.visualstudio.com/docs/cpp/config-linux
0
BroFar yes sir, but why it's different in other compilers?
0
BroFar does deallocation takes place only in GCC compilers?
0
Dennis I gotcha 👍