- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am trying to compile a small test program (see code below) using the Intel Fortran Compiler Version 8.0 Build 20040122Z (latest update) on Linux (Pentium 4, SuSE 9.0) in which an external function returns a pointer as result. The compilation works ('ifort test.F90') but when I execute the binary the function doesn't seem to work as expected.
The result for 'test1' is
11073827872
which is definitely wrong.
But when I uncomment the following line in my function (I know that it is no longer a pure procedure...)
! print *, resultp
the result is
3 (print *, resultp)
3 (print *, test1).
And now I am really confused. Why is the result correct all of a sudden? Because I used the print command?
I have tested my program with two other Fortran compilers (Intel Fortran Compiler Version 7.1 Build 20030307Z and the Fujitsu Fortran Compiler). With these two I get the correct result (without have to print it in the function). So, is this a compiler bug or am I missing something?
Frank
---------------------- Begin Code (test.F90) ------------------------------
program test
implicit none
! define explicit interface because resultp is pointer
interface get
function get(left,right) result(resultp)
integer, intent(in) :: left, right
integer, pointer :: resultp
integer, target :: result_add
end function get
end interface
! define variables
integer, pointer :: test1
integer :: test2, test3
! initialise variables with zero/NULL
test1 => NULL()
test2 = 0
test3 = 0
! assign values
test2 = 1
test3 = 2
! call external function
test1 => get(test2,test3)
! print result
print *, test1
end program test
function get(left,right) result(resultp)
integer, intent(in) :: left, right
integer, pointer :: resultp
integer, target :: result_add
resultp => NULL()
result_add = 0
result_add = left + right
resultp => result_add
! print *, resultp
return
end function get
---------------------------- End Code -------------------------------------
I am trying to compile a small test program (see code below) using the Intel Fortran Compiler Version 8.0 Build 20040122Z (latest update) on Linux (Pentium 4, SuSE 9.0) in which an external function returns a pointer as result. The compilation works ('ifort test.F90') but when I execute the binary the function doesn't seem to work as expected.
The result for 'test1' is
11073827872
which is definitely wrong.
But when I uncomment the following line in my function (I know that it is no longer a pure procedure...)
! print *, resultp
the result is
3 (print *, resultp)
3 (print *, test1).
And now I am really confused. Why is the result correct all of a sudden? Because I used the print command?
I have tested my program with two other Fortran compilers (Intel Fortran Compiler Version 7.1 Build 20030307Z and the Fujitsu Fortran Compiler). With these two I get the correct result (without have to print it in the function). So, is this a compiler bug or am I missing something?
Frank
---------------------- Begin Code (test.F90) ------------------------------
program test
implicit none
! define explicit interface because resultp is pointer
interface get
function get(left,right) result(resultp)
integer, intent(in) :: left, right
integer, pointer :: resultp
integer, target :: result_add
end function get
end interface
! define variables
integer, pointer :: test1
integer :: test2, test3
! initialise variables with zero/NULL
test1 => NULL()
test2 = 0
test3 = 0
! assign values
test2 = 1
test3 = 2
! call external function
test1 => get(test2,test3)
! print result
print *, test1
end program test
function get(left,right) result(resultp)
integer, intent(in) :: left, right
integer, pointer :: resultp
integer, target :: result_add
resultp => NULL()
result_add = 0
result_add = left + right
resultp => result_add
! print *, resultp
return
end function get
---------------------------- End Code -------------------------------------
Message Edited by cfroemmel@hotmail.com on 02-13-2004 04:54 AM
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It appears you are creating a pointer to a temporary variable,
the automatic local.
When the routine returns, the pointer is now in an undefined state.
The value it then prints out is undefined.
If you want to have memory which persists after a call, you will need to ALLOCATE() the pointer.
Note I'm not a language lawyer, just a user.
the automatic local.
When the routine returns, the pointer is now in an undefined state.
The value it then prints out is undefined.
If you want to have memory which persists after a call, you will need to ALLOCATE() the pointer.
Note I'm not a language lawyer, just a user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The reason of your problem is that the variable result_add is
not saved in the get function.
If you replace:
integer,target :: result_add
by
integer, save,target :: result_add
inside the function, the correct result is returned.
Regards,
Jean Vezina

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page