Difference between revisions of "Goto"

From Free Pascal wiki
Jump to navigationJump to search
m (line number in link)
m (syntaxhighlight offset)
Line 8: Line 8:
 
As an example, here an excerpt from FPC's code base [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/extres.inc?revision=37113&view=markup#l324 <tt>rtl/inc/extres.inc</tt>]:
 
As an example, here an excerpt from FPC's code base [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/extres.inc?revision=37113&view=markup#l324 <tt>rtl/inc/extres.inc</tt>]:
 
<syntaxhighlight lang="pascal" line start="324">procedure InitResources;</syntaxhighlight>
 
<syntaxhighlight lang="pascal" line start="324">procedure InitResources;</syntaxhighlight>
<syntaxhighlight lang="pascal" line start="328" highlight="328">label ExitErrMem, ExitErrFile, ExitNoErr;
+
<syntaxhighlight lang="pascal" line start="328" highlight="1">label ExitErrMem, ExitErrFile, ExitNoErr;
 
begin</syntaxhighlight>
 
begin</syntaxhighlight>
<syntaxhighlight lang="pascal" line start="339" highlight="340">  ResHeader:=GetMem(sizeof(TExtHeader));
+
<syntaxhighlight lang="pascal" line start="339" highlight="2">  ResHeader:=GetMem(sizeof(TExtHeader));
 
   if ResHeader=nil then goto ExitErrFile;</syntaxhighlight>
 
   if ResHeader=nil then goto ExitErrFile;</syntaxhighlight>
<syntaxhighlight lang="pascal" line start="366" highlight="371">  goto ExitNoErr;
+
<syntaxhighlight lang="pascal" line start="366" highlight="6">  goto ExitNoErr;
  
 
   ExitErrMem:
 
   ExitErrMem:

Revision as of 17:39, 11 April 2018

Deutsch (de) English (en) français (fr) русский (ru)

goto is an unconditional jump to a previously declared label (either before or after the goto command).

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 is 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:

324procedure InitResources;
328label ExitErrMem, ExitErrFile, ExitNoErr;
329begin
339  ResHeader:=GetMem(sizeof(TExtHeader));
340  if ResHeader=nil then goto ExitErrFile;
366  goto ExitNoErr;
367
368  ExitErrMem:
369    FreeMem(ResHeader);
370    ResHeader:=nil;
371  ExitErrFile:
372    {$I-}
373    Close(fd);
374    {$I+}
375  ExitNoErr:
376end;

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