Re: Segmentation Fault when passing certain size array in a function



On 03/30/2011 01:33 PM, Thomas Jahns wrote:
This is an almost perfect example of why C does not require as much stack space:
because of the pass-by-reference semantic of Fortran, every invocation of x
consumes stack space. The corresponding C program can be optimized into a
version that does not recurse at all and runs as long as permitted.\
[...]
static void
x(int i)

That's not really equivalent as it uses pass by value. The equivalent Fortran program would be the following - using VALUE:

call x(1)
contains
recursive subroutine x(i)
use iso_fortran_env
integer, value :: i
if (mod(i, 1000000) == 0) write(error_unit,'(a,i0)')'i=', i
call x(i+1)
end subroutine x
end


In principle, the compiler could as easily transform this into a loop as it could do for the C program. However, at least in case of GCC 4.7, (the internal representation of) the WRITE statement seems to confuse the middle-end and thus the optimization is not done.* (ifort also does not convert it into a loop.)

Thus, I do not see this as fundamental advantage.

Tobias

* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48363
.