Can Goto Traverse Functions Without Invoking Destructors?
A common misconception persists that goto skips sections of code, obviating the execution of destructors and similar operations. This misapprehension is particularly pronounced in discussions surrounding C .
Scope Restrictions
Contrary to popular belief, goto cannot traverse function boundaries. Attempting to jump to a label defined in a different function results in a compiler error, as illustrated below:
void f() { int x = 0; goto lol; } int main() { f(); lol: return 0; }
Error: label 'lol' used but not defined
Object Initialisation Considerations
Jumping across object initialisation is similarly disallowed. If you attempt to skip the initialisation of an object, the compiler will flag an error:
int main() { goto lol; int x = 0; lol: return 0; }
Error: jump to label ‘lol’
Error: from here
Error: crosses initialization of ‘int x’
Scope-Specific Destruction
Objects with automatic storage duration are destroyed when exiting their scope, regardless of the method of exit. Goto, therefore, cannot bypass object destruction:
struct T { T() { cout << "*T"; } ~T() { cout << "~T"; } }; int main() { { T t; goto lol; } lol: return 0; }
Output: *T~T
Object Type Restrictions
While jumping into the scope of non-scalar objects is generally prohibited, there are exceptions for objects with trivial default constructors and destructors. Array declarations without initialisers are also exempt.
Conclusion
Goto, while not a loophole that circumvents C 's scoping mechanisms, can cause unforeseen behaviour if used carelessly. Nevertheless, the restrictions outlined above provide safeguards against unintentional or harmful goto usage. Therefore, it is not inherently evil as commonly perceived but should be applied with discretion.
The above is the detailed content of Does `goto` Bypass Destructor Execution in C ?. For more information, please follow other related articles on the PHP Chinese website!