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)
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

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.



Relevant Pages

  • Re: About alternatives to Matlab
    ... But whether a single loop is faster than several BLAS calls does not ... Bad Fortran 95 can easily be 25% lower than well-tuned C99. ... NumPy can be slower than equivalent C by an order of magnitude. ... manipulated within Python, not the speed of python code using NumPy ...
  • Re: Coding in Fortran
    ... I would like to start some programming in fortran. ... a loop got performed at least once. ... That f77 feature was one of the last to be implemented uniformly. ...
  • Re: should every thing be zero indexed?
    ... Jim Rogers wrote: ... >> This for loop would have the structure of a quantification: ... >> as, arrays. ... > I acknowledge that the concept of arrays was used heavily in Fortran, ...
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... >> both less efficient and less safe than the Fortran and Basic standard. ... >> The C for loop is actually trying to do what a do loop does. ... sloppy thinking that results from confusing a programming language ... > I do not believe that you are capable of writing a conforming C compiler. ...
  • Re: dynamic type in function calls?
    ... using the C preprocessor and #define with Fortran. ... Compile time expansion could probably also be done with preprocessor ... The old favorite example would do loop ... unrolling for small loop sizes, ...