- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I'm new in the field of pointers and I wonder a little bit on one thing. Here is my example:
[fortran]
program main_program
interface
subroutine sub_ptr(b)
real, dimension (:), pointer :: b
end subroutine sub_ptr
end interface
!
real, dimension (:), pointer :: a
!
call sub_ptr(a)
write(*,'(<size(a)>f6.2)') a
end program main_program
[/fortran]
[fortran]
subroutine sub_ptr(b)
real, dimension (:), pointer :: b
real, dimension (:), allocatable, target :: c
integer, parameter :: m = 5
integer :: i
allocate (b(m))
allocate (c(m))
do i = 1, m
c(i) = float(i)
end do
b = c ! this is how it works
!b => c ! this is how it not works
continue
end subroutine sub_ptr
[/fortran]
If I assign b to c by => in the subroutine, the result of a in the main program is garbage. Doing the same just with the equal sign, a contains the expected data...
Is this caused by the fact, that all variables of the subroutine are freed, if the subroutine is finished? Then the pointer points into nirvana?
Best regards, Johannes
ps: In ifort XE 12.1.6.369 and VS 2010 SP1 the array c is not correctly shown in debug mode, when hovering the mouse on it: "Undefined pointer/ array" Also in watch window. Is this a bug?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Johannes wrote:Yes. If you want the target to stick around after the routine is done, add the save attribute
If I assign b to c by => in the subroutine, the result of a in the main program is garbage. Doing the same just with the equal sign, a contains the expected data...
Is this caused by the fact, that all variables of the subroutine are freed, if the subroutine is finished? Then the pointer points into nirvana?
Johannes wrote:If you are hovering over the array c before it has been allocated, it is indeed undefined. The memory for c has not been allocated yet. This is not a bug. Regards, Annalee Intel Developer Support
ps: In ifort XE 12.1.6.369 and VS 2010 SP1 the array c is not correctly shown in debug mode, when hovering the mouse on it: "Undefined pointer/ array" Also in watch window. Is this a bug?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
jimdempseyatthecove wrote:Not here. That only works if b is an allocatable, not a pointer. In the above example, if b doesn't have a target the same shape is c, then the assignment is an error. But in that example b did receive a target (via allocation) so the ordinary assignment works. With pointer assignment, memory is leaked because the target b received by allocation no longer has any pointer to it. This could be fixed by simply not allocating a target for b before the pointer assignment. As has been mentioned previously, this could be fixed by giving local variable c in subroutine sub_ptr the SAVE attribute, but this could get complicated if subroutine sub_ptr were called again. First, the allocation of c would be an error, and then if c were first deallocated, pointer a in program main_program would have undefined allocation status, or if c were used without reallocating, it couldn't take on a different shape after the first call and the target of pointer a in program main_program would be mysteriously altered. Maybe it's best to simply declare c as a pointer or work with b directly.The b = c format (when realloc lhs in effect) will allocate caller's b via the subrotine reference to b.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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