- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello!
Are there limitations by calling recursive subroutines?
I tried the following very simple program and after 18421 times of calling the routine I got an error message.
Code:
-------
(main program)
implicit none
integer(2) :: n
n = 1
write(*,*) 'begin'
call factorial(n)
END
(recursive subroutine)
RECURSIVE SUBROUTINE factorial (n)
integer(2), intent(inout) :: n
write(*,*) 'n: ', n
n = n + 1
call factorial (n)
END SUBROUTINE
Error message:
------------------
Unhandled exception at 0x7d61f448 in test.exe: 0xC0000005: Access violation writing location 0x00030e3c.
There is no source code available for the current location.
Thanks for your help,
Hannes
Are there limitations by calling recursive subroutines?
I tried the following very simple program and after 18421 times of calling the routine I got an error message.
Code:
-------
(main program)
implicit none
integer(2) :: n
n = 1
write(*,*) 'begin'
call factorial(n)
END
(recursive subroutine)
RECURSIVE SUBROUTINE factorial (n)
integer(2), intent(inout) :: n
write(*,*) 'n: ', n
n = n + 1
call factorial (n)
END SUBROUTINE
Error message:
------------------
Unhandled exception at 0x7d61f448 in test.exe: 0xC0000005: Access violation writing location 0x00030e3c.
There is no source code available for the current location.
Thanks for your help,
Hannes
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - hgr_iwf
Hello!
Are there limitations by calling recursive subroutines?
I tried the following very simple program and after 18421 times of calling the routine I got an error message.
Code:
-------
(main program)
implicit none
integer(2) :: n
n = 1
write(*,*) 'begin'
call factorial(n)
END
(recursive subroutine)
RECURSIVE SUBROUTINE factorial (n)
integer(2), intent(inout) :: n
write(*,*) 'n: ', n
n = n + 1
call factorial (n)
END SUBROUTINE
Error message:
------------------
Unhandled exception at 0x7d61f448 in test.exe: 0xC0000005: Access violation writing location 0x00030e3c.
There is no source code available for the current location.
Thanks for your help,
Hannes
Are there limitations by calling recursive subroutines?
I tried the following very simple program and after 18421 times of calling the routine I got an error message.
Code:
-------
(main program)
implicit none
integer(2) :: n
n = 1
write(*,*) 'begin'
call factorial(n)
END
(recursive subroutine)
RECURSIVE SUBROUTINE factorial (n)
integer(2), intent(inout) :: n
write(*,*) 'n: ', n
n = n + 1
call factorial (n)
END SUBROUTINE
Error message:
------------------
Unhandled exception at 0x7d61f448 in test.exe: 0xC0000005: Access violation writing location 0x00030e3c.
There is no source code available for the current location.
Thanks for your help,
Hannes
When you call a recursive function, it takes space in the stack at each call (in your case dont forget that "write" use stack space). So it comes a point where the stack is full.
In your case, you don't have exit condition so it cannot ends. So the program will crash on stack overflow or integer overflow when "n" will reach 2^31.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - gvautier
Hello
When you call a recursive function, it takes space in the stack at each call (in your case dont forget that "write" use stack space). So it comes a point where the stack is full.
In your case, you don't have exit condition so it cannot ends. So the program will crash on stack overflow or integer overflow when "n" will reach 2^31.
When you call a recursive function, it takes space in the stack at each call (in your case dont forget that "write" use stack space). So it comes a point where the stack is full.
In your case, you don't have exit condition so it cannot ends. So the program will crash on stack overflow or integer overflow when "n" will reach 2^31.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page