| I didn't understand why my point was downvoted, anybody who likes to know how things work on backstage or likes to hack stuff would find it interesting. Sure, undefined behavior can assume anything, but the compiler writer still has to respect the ABI, right? I was reading the Itanium C++ ABI draft and the only thing I found about it is: "If a class has a non-virtual destructor, and a deleting destructor is emitted for that class, the deleting destructor must correctly handle the case that the this pointer is NULL. All other destructors, including deleting destructors for classes with a virtual destructor, may assume that the this pointer is not NULL."
https://mentorembedded.github.io/cxx-abi/abi.html However, you can see that I don't have an instance, there is no object so the destructor won't be called. It means that the compiler writer won't probably consider checking 'this' pointer for null - clang/gcc both don't care: % g++-4.9 -Wall -O3 -std=c++11 test.cpp -o test; ./test
0
21
% clang++ -Wall -O3 -std=c++11 test.cpp -o test; ./test
0
21
0x400886 <main()> push %rbp
0x400887 <main()+1> mov %rsp,%rbp
0x40088a <main()+4> mov $0xb,%edx
0x40088f <main()+9> mov $0xa,%esi
0x400894 <main()+14> mov $0x0,%edi
0x400899 <main()+19> callq 0x400910 <myclass::sum(int, int)>
The 'this' pointer is just a parameter in the register EDI.NOTE: I don't recommend anyone writing code like this. I'm always concerned about UB and I agree with you. But it's still interesting to know how things really are. |