- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
Hi to everyone. I've found a problem illustrates as follows:
module mod
type :: test
integer :: i
end type test
contains
subroutine equals(left, right)
type(test) :: left
type(test) :: right
write(*,*) loc(p), loc(q)
end subroutine equals
end module mod
program main
use mod
type(test), target :: tar
type(test), pointer :: p=>null(), q=>null()
allocate(p, q)
write(*,*) loc(p), loc(q) ! First
p => tar
q => tar
write(*,*) loc(p), loc(q) ! Second
call equals(p,q) ! Third
end program main
The First and Second WRITE statements work fine and loc(p) and loc(q) equal to each other in the Second part. But in the Third time, the returned WRITE statement within the SUBROUTINE outputs a wired result which loc(p) and loc(q) were no longer identical. Is there anything wrong with the code? And if so, what are the returned values in the Third WRITE statement considering that they are not the location of pointer p and q which should be equal then?
Ссылка скопирована
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
Add IMPLICIT NONE at the beginning of the subroutine, and you will see for yourself.
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
Which version of the compiler did you use? I do not get the errors that you reported -- with IFort 15.0.2.
It may not matter in this small example, but there is a potential problem in the code: you allocated the two pointer variables, and then re-associated the pointers with something else. Once you do this, you have an irreparable memory leak.
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
I'm using Intel(R) Visual Fortran Compiler XE 13.0 Update 1 for Windows*. So is it to say that the difference between two versions of compilers caused the result? By the way, I awared of the memory leak problem, thanks mecej4. And can anyone suggest a safe way to avoid it when declaring and initializing pointers?
- Подписка на RSS-канал
- Отметить тему как новую
- Отметить тему как прочитанную
- Выполнить отслеживание данной Тема для текущего пользователя
- Закладка
- Подписаться
- Страница в формате печати