Home > Backend Development > C++ > Does `goto` Bypass Destructor Execution in C ?

Does `goto` Bypass Destructor Execution in C ?

Linda Hamilton
Release: 2024-12-01 04:52:14
Original
894 people have browsed it

Does `goto` Bypass Destructor Execution in C  ?

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template