0

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)

  1. Don't initialize the type
  2. Don't declare table1 as an array
  3. Make the component col allocatable

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?

7
  • 1
    Still, making your array allocatable or using compile flags to create the array in the heap is the way to go. Commented Aug 24, 2018 at 12:36
  • 1
    The code you show is not the code you compiled. Commented Aug 24, 2018 at 16:58
  • @Steve Thanks and sorry about that. Typo on 2nd last line, fixed now. I had table(1) but was supposed to be table1(1) Commented Aug 24, 2018 at 19:00
  • @JohnE, what exactly are you trying to demonstrate? You've found another way to exhaust your stack! Your code compiles fine with gfortran 6.4.1, 7.3.1, 8.1.1, and 9.x and executes. Of course, my default stack size is 1 GB. This has absolutely nothing to do with Fortran let alone gfortran. BTW, if you turning on optimization, then program should execute for you as DCE occurs. Commented Aug 24, 2018 at 20:24
  • @steve I edited the question in an attempt to answer your request for clarification. Please feel free to answer in a question in detail, if you are so inclined. Apologies if it is a dumb question but I can tell you this is a sincere attempt to understand the root causes and what are the best practices for avoiding (not just quick fixes) Commented Aug 25, 2018 at 8:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.