As the title suggests, I'm getting a seg fault related to use of a derived type component. This is a followup to Segmentation fault for array, but only if a component of a derived type but represents a more realistic use case. In contrast to the prior question, there is no issue of lack of explicit interface (the example below is a single program with no subroutines, and results are the same if the program is modularized in a more realistic use case).
! gfortran 4.8.5 on Red Hat linux
! default compile with no extra flags: "gfortran source.f90"
! default stack size = 8mb (ulimit -s)
program main
type table
real :: col(2100000) = 0.0 ! but OK if no initial value
end type table
type(table) :: table1(2) ! but OK if not an array
table1(1)%col = 1.0 ! segfault here
end program main
In a more realistic use case (and in my actual one), the type table would defined in a module, I just combined it all in the same place for conciseness. (I did test it this way (with the type definition in a module) and the exact same seg fault occurred.)
There are at least 3 changes here that eliminate the segfault (only the last of which works very well for my purposes)
- Don't initialize the type
- Don't declare
table1as an array - Make the component
colallocatable
The key (and most surprising thing to me) seems to be that it is declaring table1 as an array that seems to fool the compiler in some way here. Also, being a component of a derived type is an essential aspect.
For an answer I'm just looking for a general explanation of what is happening here (including if this is some kind of bug) and general recommendations on avoiding this problem -- in particular, what are the best practices to follow here that would hopefully help me avoid this issue.
Note that this issue in part relates to the stack size (e.g. ulimit -s) and shell interaction, but that is addressed pretty well in the answer to the preceding question and I'm hoping to examine other aspects of the issue here -- which is not to minimize that issue, but rather to also include other aspects.
Edit to add: @Steve comments that "This has absolutely nothing to do with Fortran let alone gfortran". This is maybe the crux of my question. I'd expect a fortran compiler to be able to handle purely static declarations as I use here but maybe I expect too much. Is the root issue perhaps a mis-match between the environment (i.e. ulimit -s (8mb)) and the compiler flags? Do the compilers (gfortran or others) have no way to query the environment or figure this out automatically?
I appreciate in @ripero's answer to the predecessor question that increasing stack size can be helpful although that's somewhat incomplete to me in the sense that the problem would not necessarily seem to be the stack size but rather some sort of mis-match between the stack size (OS or environment level issue) and compiler options like -fmax-stack-var-size?
table(1)but was supposed to betable1(1)