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

Question: How to judge whether a pointer points to some target?

Zhanghong_T_
Novice
1,055 Views
In CVF, I judge whether a pointer points to some target by following:
integer, target:: j
integer::locj
integer, pointer:: pj
pj=>j
locj=loc(j)
if(locj==loc(pj))then
...
endif
But in IVF, the way sometime doesn't feasible, although it can get a correct result for the above code. Here I have attached two pictures to show this. For the same source code, I compiled and ran in IVF and CVF and got two different result. What I want is the result in CVF of course.
Can anyone give me other ways to realize my aim?
Thanks,
Zhanghong Tang
0 Kudos
11 Replies
Jugoslav_Dujic
Valued Contributor II
1,055 Views

The standard way to check whether pj points to j is:

if (ASSOCIATED(pj, j))

however, the method with LOC() you described should work as well.

I don't see the essential difference between the pictures you attached. You cannot expect the addresses to be the same; they can be pretty much anything. Is there something that I'm missing?

You probably know that a pointer has to be explicitly initialized before you try to query ASSOCIATED or LOC,like any other variable, don't you? (Otherwise, you'll get random result):

integer, pointer:: p

if (associated(p)) then
write (*,*) loc(p)
end if

Write line will get executed randomly, depending on compiler, environment, and phase of the Moon, as it has "undefined" association status. p must be explicitly NULLIFYed orassociated with somethingbefore you do anything with it.

Jugoslav

0 Kudos
Zhanghong_T_
Novice
1,055 Views

Thank you very much!

Of course I know the adresses are not the same, the difference is: in CVF, when the cycle ended, the value ofI is 3 but in IVF the value of I is 4 (see the watch window below). That means in CVF I can find a pointer which points to some target, but in IVF I can't find!

Sincerely,

Zhanghong Tang

0 Kudos
Zhanghong_T_
Novice
1,055 Views
In addition, I am sure that I have NULLIFYed orassociated some pointer when refer it.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,055 Views
OK I see now. But what you observe doesn'tlook like comparison problem -- it's obvious that in CVF, btri%nbr(3)%p is equal to loctri1, while in IVF it's not. The root of the problem (judging on the data I have) appears to be somewhere else. It's rather difficult for a casual observer to see what's going on in the code snippet displayed, as list and tree walking codes tend to be ratherdifficult to comprehend & debug.
Of course, that still might be a compiler bug, butthere's no indicationit's in the code you showed.
Jugoslav
0 Kudos
Zhanghong_T_
Novice
1,055 Views
I have tried your way, but it failed in CVF. I think judge by its address is more feasible.If a pointer pjpoints to a target j, loc(pj) must equals to loc(j), but if loc(pj)=loc(j), pj is not necessary associated to j.
So it seems that I still have tofind how to solve my problem in IVF.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,055 Views
but if loc(pj)=loc(j), pj is not necessary associated to j.
Hmm, I don't think so. I'd think that ASSOCIATED(pj, j) does exactlythe check of LOCs(it's more complicated if pj is an array) -- if you don't mind portability, LOC's are ok though.
Jugoslav
0 Kudos
Zhanghong_T_
Novice
1,055 Views

You mean loc(pj)=loc(j) is equivalent withpj is associated to j?

But I think there are still difference between IVF and CVF's compilers (I dare not say IVF's compiler has bug).

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,055 Views

You mean loc(pj)=loc(j) is equivalent withpj is associated to j?

Yes. Actually, it's equivalent to:

program Assoc

integer, target:: j
integer, pointer:: pj
logical:: b

pj=>j

b = loc(pj).eq.loc(j) .and. loc(pj).ne.0
b = associated(pj, j)

end program Assoc

Assembly output of the two lines is identical:

9: b = loc(pj).eq.loc(j) .and. loc(pj).ne.0
00401029 lea edx,[J (004462f4)]
0040102F mov ecx,dword ptr [PJ (004462e8)]
00401035 xor eax,eax
00401037 cmp ecx,edx
00401039 sete al
0040103C mov edx,dword ptr [PJ (004462e8)]
00401042 xor ecx,ecx
00401044 cmp edx,0
00401047 setne cl
0040104A and ecx,eax
0040104C neg ecx
0040104E mov dword ptr [B (004462f0)],ecx
10: b = associated(pj, j)
00401054 lea edx,[J (004462f4)]
0040105A mov eax,dword ptr [PJ (004462e8)]
00401060 xor ecx,ecx
00401062 cmp eax,edx
00401064 sete cl
00401067 mov eax,dword ptr [PJ (004462e8)]
0040106D xor edx,edx
0040106F cmp eax,0
00401074 setne dl
00401077 and edx,ecx
00401079 neg edx
0040107B mov dword ptr [B (004462f0)],edx

(I dare not say IVF's compiler has bug).

Neither do I :-), but we both witnessed it isn't quite bug free.

Jugoslav

0 Kudos
Zhanghong_T_
Novice
1,055 Views

I am fool. When I want to know the address of a pointer, I always type

loci=loc(ip)

Now I have a simple way:)

Thanks!

For your answer, I think it maybe right in this code, but when I replace

loc(pj)==loc(j)

with

associated(pj, j)

the program ran error.

Of course in my code pj and j is more complicated than a simple integer pointer.

Maybe I have done something wrong.

0 Kudos
Zhanghong_T_
Novice
1,055 Views
rere
0 Kudos
Zhanghong_T_
Novice
1,055 Views
re
0 Kudos
Reply