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

assumed shape array, explaination please!

jft27
Beginner
485 Views
Hi there,
could someone explain to me why the attatched code ends with a segmentation fault? It doesn't seem like a big enough array to be causing memory type problems, at least it doesn't to me!

program testmatrix


use module_2


implicit none

real, dimension(:, :), allocatable :: matrix


allocate(matrix(3000, 3000))


matrix = 1.0

call trythis(matrix)


deallocate(matrix)



end program testmatrix

module module_2

use module_1


implicit none


contains


subroutine trythis(x)




real, dimension(:, :) :: x


print*, x






end subroutine trythis






end module module_2



thanks

Jeremy
0 Kudos
6 Replies
Steven_L_Intel1
Employee
485 Views
You're getting a stack overflow on the assignment to the array. I guess the compiler is constructing a temporary array for the constant 1.0. Increase the stacklimit to work around this.
0 Kudos
jft27
Beginner
485 Views
Thank you! I didn't realize this would affect arrays that small.
Jeremy
0 Kudos
hajek
Beginner
485 Views
I do not think the compiler creates temporary for the array asignment (that would be silly, anyway). Check out the following code:

module module_2
implicit none
contains
subroutine trythis(x)
real, dimension(:, :) :: x
open(10,file='test.out')
print *,'file opened'
write(10,*) x
print *,'array written'
close(10)
end subroutine

subroutine trythis2
real, dimension(:, :) :: x
allocatable:: x
allocate(x(3000,3000))
x = 1.0
open(10,file='test.out')
write(10,*) x
close(10)
end subroutine

end module module_2

program testmatrix
use module_2
implicit none
real, dimension(:, :), allocatable :: matrix
allocate(matrix(3000, 3000))
matrix = 1.0
call trythis2
print *,'trythis2 returned'
call trythis(matrix)
deallocate(matrix)
end program

compiled with ifort 9.0 (default settings), it produces
the output:

trythis2 returned
file opened
Segmentation fault

(and creates a huge file).
The fault evidently occurs at write, and the strange thing is that it doesn't in trythis2, where an array of the same size is written. Perhaps this is a bug after all?
0 Kudos
TimP
Honored Contributor III
485 Views
This example has no problem running on my 3 year old laptop, with current ifort 9.1.
0 Kudos
hajek
Beginner
485 Views
OK, so it is probably fixed already in 9.1
0 Kudos
joseph-krahn
New Contributor I
485 Views
There were definitely some bugs fixed for allocatable arrays passed as a subroutine argument. There are (or were) cases where allocatable arguments end up requiring a temporary stack array. I would not be suprised if there are not still a few issues with certain uses of allocatable arrays, especially when using the features that are extensions to F95.
0 Kudos
Reply