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; }

26th Aug 2022, 2:55 AM
Ujjawal Gupta
Ujjawal Gupta - avatar
9 Respostas
26th Aug 2022, 3:28 AM
BroFar
BroFar - avatar
+ 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.
26th Aug 2022, 7:11 AM
Dennis
Dennis - avatar
+ 4
Ujjawal Gupta I'm not getting the same thing with your code so I'm confused https://code.sololearn.com/czL2iyA1Rx7a/?ref=app
26th Aug 2022, 3:04 AM
BroFar
BroFar - avatar
+ 2
Ujjawal Gupta but you do see that here you are getting the correct answer - true ?!
26th Aug 2022, 3:09 AM
BroFar
BroFar - avatar
+ 1
BroFar am using clang version 9.0.1 to compile c++17 and getting 578 two times
26th Aug 2022, 3:08 AM
Ujjawal Gupta
Ujjawal Gupta - avatar
+ 1
Here you are using g++ 10 Ubuntu Ujjawal Gupta on vs code editor. https://code.visualstudio.com/docs/cpp/config-linux
26th Aug 2022, 3:25 AM
BroFar
BroFar - avatar
0
BroFar yes sir, but why it's different in other compilers?
26th Aug 2022, 3:14 AM
Ujjawal Gupta
Ujjawal Gupta - avatar
0
BroFar does deallocation takes place only in GCC compilers?
26th Aug 2022, 3:27 AM
Ujjawal Gupta
Ujjawal Gupta - avatar
0
Dennis I gotcha 👍
26th Aug 2022, 8:13 AM
Ujjawal Gupta
Ujjawal Gupta - avatar