Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29249 Discussions

basics of optional arguments (Was posted as: "weird SIGSEGV with optional arguments")

mrentropy1
Beginner
668 Views

Tell me I'm doing something stupid here:
[plain]module mod1
implicit none
contains

subroutine bar(k)
integer, optional :: k
#ifdef FAIL
k = 1
#endif
end subroutine bar
!
end module mod1

program main
!
use mod1
call bar()
!
end program main
[/plain]

OK now compile and run w/o FAIL being set:

[plain]pwill@zarathustra:~/samba$ ifort pain.F90
pwill@zarathustra:~/samba$ ./a.out
pwill@zarathustra:~/samba$


[/plain]

OK now redo compile/run with -DFAIL flag set, to include the code that would otherwise be commented out:

[plain]pwill@zarathustra:~/samba$ ifort pain.F90 -DFAIL
pwill@zarathustra:~/samba$ ./a.out
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
a.out 0000000000402B24 Unknown Unknown Unknown
a.out 0000000000402ADC Unknown Unknown Unknown
libc.so.6 00007F9F1941E5A6 Unknown Unknown Unknown
a.out 00000000004029D9 Unknown Unknown Unknown
pwill@zarathustra:~/samba$
[/plain]


So, it appears that merely testing for the presence of setting an optional argument is now making the code SIGSEGV.

Am I missing something? Am I not allowed to set optional arguments?

Peter
0 Kudos
1 Solution
Kevin_D_Intel
Employee
668 Views
0 Kudos
3 Replies
mrentropy1
Beginner
668 Views

P.S. I've edited the code to make it even more succinct (as should now be reflected in posting - first version was a bit lengthy)... anyway still fails in same manner.

Oh yes: Intel 64, Version 11.1, Build 20090630 Package ID: l_cprof_p_11.1.046, on Ubuntu 9.04 x86_64.
0 Kudos
Kevin_D_Intel
Employee
669 Views

It is illegal to access a non-present optional argument. Steve has an nice discussion in The Virtues of Omission.

These previous threads contains similar discussions:

http://software.intel.com/en-us/forums/showthread.php?t=53378
http://software.intel.com/en-us/forums/showthread.php?t=53122
http://software.intel.com/en-us/forums/showthread.php?t=60046

0 Kudos
mrentropy1
Beginner
668 Views
Ahhh. Thanks. Switch that from "compiler error" to "keyboard-chair interface error". :)

I've read Steve's post as you suggest. Every other feature of optional arguments makes sense - but not being able to access them if they aren't passed, that was a surprise to me. But I guess it makes complete and total sense when you remember that Fortran is pass-by-reference and not pass-by-value (obviously Fortran isn't my first language!).

Now the SIGSEGV also makes complete sense - I'm trying to write to memory off in the middle of nowhere somewhere! I suppose that should have been a hint.....

Peter
0 Kudos
Reply