- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi
Any suggestions how to test whether two type(C_PTR) variables have the same value? It wasn't a problem when using integer(int_ptr_kind()) to hold a C pointer, but that is deprecated in F2003.
Overriding the '==' operator would seem to be the way to go, but the comparison fn can't compare the members %PTR because they are private.
Thanks
Any suggestions how to test whether two type(C_PTR) variables have the same value? It wasn't a problem when using integer(int_ptr_kind()) to hold a C pointer, but that is deprecated in F2003.
Overriding the '==' operator would seem to be the way to go, but the comparison fn can't compare the members %PTR because they are private.
Thanks
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
for this purpose, the logical function C_ASSOCIATED(c_ptr1[, c_ptr2]) is provided in the ISO_C_BINDING module.
Regards
for this purpose, the logical function C_ASSOCIATED(c_ptr1[, c_ptr2]) is provided in the ISO_C_BINDING module.
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks
C_ASSOCIATED(c_ptr1[, c_ptr2]) does it, except that it returns false if you pass it two C_NULL_PTRs.
So the test for equality is ( (.not.C_ASSOCIATED(ptr1).and. .not. C_ASSOCIATED(ptr2)) .or. C_ASSOCIATED(ptr1,ptr2))
Which runs up against a bug: in a mix-languaged app with C++ calling fortran (latest compiler versions for both) . it seems that C_NULL_PTR doesnt get initialised so that
C_ASSOCIATED(C_NULL_PTR) returns true
I've filed a bug report and intel are investigating. Has anyone else come across this?
P
C_ASSOCIATED(c_ptr1[, c_ptr2]) does it, except that it returns false if you pass it two C_NULL_PTRs.
So the test for equality is ( (.not.C_ASSOCIATED(ptr1).and. .not. C_ASSOCIATED(ptr2)) .or. C_ASSOCIATED(ptr1,ptr2))
Which runs up against a bug: in a mix-languaged app with C++ calling fortran (latest compiler versions for both) . it seems that C_NULL_PTR doesnt get initialised so that
C_ASSOCIATED(C_NULL_PTR) returns true
I've filed a bug report and intel are investigating. Has anyone else come across this?
P
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
False is the correct return for two C_NULL_PTRs. NULL pointers are never considered to be associated with each other.
I can't imagine how C_NULL_PTR could fail to be initialized as it's a global object. I'll be interested in seeing your test case.
I can't imagine how C_NULL_PTR could fail to be initialized as it's a global object. I'll be interested in seeing your test case.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
this is Issue number: 512673
here is the test example. It works fine if the caller is fortran. Both compilers are 10.1.021 [IA-32] for Windows.
#include "stdafx.h"
extern "C" int cf_codetestnoargs ( );
int _tmain(int argc, _TCHAR* argv[])
{
cf_codetestnoargs ( );
return 0;
}
function cf_codetestnoargs( ) result(iret) bind(C)
!DEC$ ATTRIBUTES ALIAS: "_cf_codetestnoargs" :: cf_codetestnoargs
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
INTEGER (c_int) :: Iret
type(C_PTR) :: myptr
logical :: a,b
a = C_ASSOCIATED(C_NULL_PTR)
myptr=C_NULL_PTR
b= C_ASSOCIATED(myptr)
write(*,*) 'C null ptr ',C_ASSOCIATED(C_NULL_PTR),' (shouldbe false) '
iret=0
write(*,*) 'Is Associated ',C_ASSOCIATED(myptr),' (shouldbe false) '
if(a .or. b) then
write(*,*) ' C_ASSOCIATED NULL PTR wrong'
iret = 1/iret ! for a crash
endif
end function cf_codetestnoargs
here is the test example. It works fine if the caller is fortran. Both compilers are 10.1.021 [IA-32] for Windows.
#include "stdafx.h"
extern "C" int cf_codetestnoargs ( );
int _tmain(int argc, _TCHAR* argv[])
{
cf_codetestnoargs ( );
return 0;
}
function cf_codetestnoargs( ) result(iret) bind(C)
!DEC$ ATTRIBUTES ALIAS: "_cf_codetestnoargs" :: cf_codetestnoargs
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
INTEGER (c_int) :: Iret
type(C_PTR) :: myptr
logical :: a,b
a = C_ASSOCIATED(C_NULL_PTR)
myptr=C_NULL_PTR
b= C_ASSOCIATED(myptr)
write(*,*) 'C null ptr ',C_ASSOCIATED(C_NULL_PTR),' (shouldbe false) '
iret=0
write(*,*) 'Is Associated ',C_ASSOCIATED(myptr),' (shouldbe false) '
if(a .or. b) then
write(*,*) ' C_ASSOCIATED NULL PTR wrong'
iret = 1/iret ! for a crash
endif
end function cf_codetestnoargs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fascinating. Also works if you link with the static libraries rather than the DLL libraries.

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