Sign in Join
Wiki copytemporary

Goto: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Goto

OldNewLine
1+`goto` is an unconditional jump to a previously declared [[Label|label]] (either before or after the `goto` command). It is a [[Reserved words|reserved word]].
2+
3+Usage of `goto` in high-level programming languages such as Pascal is highly discredited, since control structures of all sorts are available.
4+
5+The last situation a `goto` is agreed with bad grace to be a significant system error, where a “graceful exit” is better than causing a system breakdown.
6+
7+As an example, here an excerpt from FPC’s code base [`rtl/inc/extres.inc`](https://gitlab.com/freepascal.org/fpc/source/blob/release_3_2_0/rtl/inc/extres.inc#L324-376)\]:
8+
9+``` pascal
10+procedure InitResources;
11+```
12+
13+``` pascal
14+label ExitErrMem, ExitErrFile, ExitNoErr;
15+begin
16+```
17+
18+``` pascal
19+ ResHeader:=GetMem(sizeof(TExtHeader));
20+ if ResHeader=nil then goto ExitErrFile;
21+```
22+
23+``` pascal
24+ goto ExitNoErr;
25+
26+ ExitErrMem:
27+ FreeMem(ResHeader);
28+ ResHeader:=nil;
29+ ExitErrFile:
30+ {$I-}
31+ Close(fd);
32+ {$I+}
33+ ExitNoErr:
34+end;
35+```
36+
37+According to the value of [`returnNilIfGrowHeapFails`](https://www.freepascal.org/docs-html/rtl/system/returnnilifgrowheapfails.html) [`getMem`](https://www.freepascal.org/docs-html/rtl/system/getmem.html) possibly may return [[Nil|nil]]. Instead of placing *everything* in a “success”-branch, a couple `goto` instructions were chosen.
38+
39+## See also
40+
41+- [[Exit|exit]]
42+- [[sGoto|{$goto} compiler directive]]
43+- [`system.longJmp`](https://www.freepascal.org/docs-html/rtl/system/longjmp.html)
44+
45+---
46+*From the Free Pascal wiki, page "Goto", as it stood on 2022-01-24 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
47+Category: Code

Back to history