Observable behavior and undefined behavior — What happens if I don’t call a destructor?

I simply do not understand what “depends on the side effects” means. It means that it depends on something the destructor is doing. In your example, modifying *p or not modifying it. You have that dependency in your code, as the output would differ if the dctor wouldn’t get called. In your current code, the … Read more

Are destructors run when calling exit()? [duplicate]

No, most destructors are not run on exit(). C++98 ยง18.3/8 discusses this. Essentially, when exit is called static objects are destroyed, atexit handlers are executed, open C streams are flushed and closed, and files created by tmpfile are removed. Local automatic objects are not destroyed. I.e., no stack unwinding. Calling abort lets even less happen: … Read more

Under what circumstances are C++ destructors not going to be called?

Are there any other circumstances where they[destructors] will not be called? Long jumps: these interfere with the natural stack unwinding process and often lead to undefined behavior in C++. Premature exits (you already pointed these out, though it’s worth noting that throwing while already stack unwinding as a result of an exception being thrown leads … Read more

problems with Move constructor and Move overloaded assignment operator?

Your class A has several issues: Your assignment operator don’t handle self assignment and leak: A& A::operator=(const A& a) { std::cout<<“overload operator=\n”; if (this != &a) { p = a.p; delete[] s; s = new char[strlen(a.s) + 1]; strcpy(s, a.s); } return *this; } Your move doesn’t move but copy: A::A(A&& a) : p(a.p), s(a.s) … Read more

Manually destroy C# objects

You don’t manually destroy .Net objects. That’s what being a managed environment is all about. In fact, if the object is actually reachable, meaning you have a reference you can use to tell the GC which object you want to destroy, collecting that object will be impossible. The GC will never collect any object that’s … Read more