Goto
Deutsch · English · Français · Русский
goto is an unconditional jump to a previously declared label (either before or after the goto command). It is a reserved word.
Usage of goto in high-level programming languages such as Pascal is highly discredited, since control structures of all sorts are available.
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.
As an example, here an excerpt from FPC’s code base rtl/inc/extres.inc]:
procedure InitResources;label ExitErrMem, ExitErrFile, ExitNoErr;
begin ResHeader:=GetMem(sizeof(TExtHeader));
if ResHeader=nil then goto ExitErrFile; goto ExitNoErr;
ExitErrMem:
FreeMem(ResHeader);
ResHeader:=nil;
ExitErrFile:
{$I-}
Close(fd);
{$I+}
ExitNoErr:
end;According to the value of returnNilIfGrowHeapFails getMem possibly may return nil. Instead of placing everything in a “success”-branch, a couple goto instructions were chosen.
See also
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.