Jump to content

Find limit of recursion: Difference between revisions

demonstrating better handling of stack-overflow exception
m (global whitespace)
(demonstrating better handling of stack-overflow exception)
Line 126:
=={{header|COBOL}}==
{{works with|OpenCOBOL 1.1}}
<lang cobol> identification division.
program-id. mungrecurse.
data division.
working-storage section.
01 depth-counter pic 9(3).
01 install-address usage is procedure-pointer.
procedure division.
01 install-flag pic x comp-x value 0.
100-main.
01 status-code pic x(2) comp-5.
move 0 to depth-counter.
01 ind pic s9(9) comp-5.
display "Mung until no good.".
 
perform 200-mung.
 
display "No good.".
linkage section.
stop run.
01 err-msg pic x(325).
 
procedure division.
100-main.
 
set install-address to entry "300-err".
call "CBL_ERROR_PROC" using install-flag
install-address
returning status-code.
 
if status-code not = 0
display "ERROR INSTALLING ERROR PROC"
stop run.
end-if
 
move 0 to depth-counter.
display "'Mung until no good."'.
perform 200-mung.
display "'No good."'.
stop run.
 
200-mung.
add 1 to depth-counter.
display depth-counter.
perform 200-mung.</lang>
300-err.
entry "300-err" using err-msg.
perform varying ind from 1 by 1
until (err-msg(ind:1) = x"00") or (ind = length of err-msg)
continue
end-perform
 
display err-msg(1:ind).
 
*> room for a better-than-abrupt death here.
exit program.</lang>
200-mung.
Compiled with <pre>cobc -free -x -g mungrecurse.cbl</pre> gives, after a while,
add 1 to depth-counter.
display depth-counter.
perform 200-mung.</lang>
Compiled with <pre>cobc -x -g mung.cbl</pre> gives, after a while,
<pre>...
249
Line 150 ⟶ 183:
252
253
mung.cbl:17Trapped: libcobrecurse.cob:38: Stack overflow, possible PERFORM depth exceeded</pre>
recurse.cob:50: libcob: Stack overflow, possible PERFORM depth exceeded</pre>
 
Without stack-checking turned on (achieved with -g in this case), it gives
Line 160 ⟶ 194:
253
254
255
256
257
Attempt to reference unallocated memory (Signal SIGSEGV)
Abnormal termination - File contents may be incorrect</pre>
which suggests that -g influences the functionality of CBL_ERROR_PROC
 
Thanks to Brian Tiffin for his [http://www.opencobol.org/modules/newbb/viewtopic.php?viewmode=thread&topic_id=254&forum=1&post_id=1312#1312 demo code on opencobol.org's forum]
 
=={{header|C sharp|C#}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.