jhaiduce
jhaiduce

Reputation: 438

Segmentation fault when passing a derived type as an optional argument

The following code produces a segmentation fault when compiled with gfortran:

module my_exceptions

  implicit none

  type::error_container
  end type error_container

contains

  subroutine throw(status)
    type(error_container), intent(out), optional::status
  end subroutine throw

  subroutine test_throw(status)
    class(error_container), intent(out), optional::status

    call throw(status)

  end subroutine test_throw

end module my_exceptions

program test
  use my_exceptions, ONLY: error_container, test_throw
  implicit none
  type(error_container)::status

  call test_throw()

end program test

It appears that gfortran is trying to do something with the status argument even though it is not present. The segmentation fault does not occur if the status is passed to test_throw(). Also, this code works as expected when compiled with ifort. Can anyone help me figure out what causes the segmentation fault?

Upvotes: 2

Views: 182

Answers (1)

jhaiduce
jhaiduce

Reputation: 438

Should have used type(error_container) instead of class(error_container) since error_container is a concrete type. Thanks to @King for pointing this out!

Upvotes: 1

Related Questions