0

Can someone explain why the following code produces a segmentation fault when ND is big (1000000)? Increasing the stack limit does not help.

module parametros_mod
  integer, parameter :: ND = 1000000
end module parametros_mod

module data_mod
  use parametros_mod
  implicit none
  private

  type, public :: data_vec
    real(8) :: f0
    real(8), dimension(ND) :: fvec
  end type data_vec
end module data_mod

!test program
program test
  use parametros_mod
  use data_mod
  implicit none

  type(data_vec) :: v1
  real(8), dimension(ND) :: rv, xv

  rv = 0d0
  rv(2) = 1d0

  v1 = data_vec(1.1d0,rv)
  xv = v1%fvec
  write(*,'(*(f0.4,2x))') xv(1:3)
end program test
2
  • Really, someone out there must be teaching the real(8) thing on massive scale. It does not come from good textbooks or tutorials. Commented Mar 2, 2017 at 20:37
  • Your code does not crash for me with gfortran 5.3. If it really does for you I would look at the data_vec(1.1d0,rv) expression, perhaps it causes a temporary object with your compiler. Tell us more about your compiler, compiler flags, operating system. Commented Mar 2, 2017 at 20:39

1 Answer 1

1

I replicated your error, but I had to use larger ND. It is really line

v1 = data_vec(1.1d0,rv)

Probably a temporary object is created on the stack.

You can avoid that by assigning the compnents

v1%f0 = 1.1d0
v1%fvec = rv
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Vladimir, it is working now. I am compiling with GNU Fortran (Debian 4.9.2-10) 4.9.2.
I found an alternative solution here in SO in question: What does “ulimit -s unlimited” do?
You said that increasing the stack size didn't help. It's up to you, I like my programs to be robust, because I run them on many different computers and I don't want to care about the stack size on each of them.

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.