diffptr_t fortran与iso_c_bindings [英] diffptr_t fortran with iso_c_bindings

查看:355
本文介绍了diffptr_t fortran与iso_c_bindings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用iso_c_bindings在fortran中有一种diffptr_t。内存距离结果必须是带符号的int。

  type(c_ptr):: start,ref 
type(c_int ):: res
start = c_loc(my_struct%a)
ref = c_loc(my_struct%b%c)
res = start-ref



编译错误:

 此二元操作无效对于这种数据类型。 
在此上下文中需要算术或LOGICAL类型。

谢谢

解决方案你不能在标准的Fortran中做指针运算。您必须依赖指针和整数之间的处理器相关二进制对应关系。



另外,在Fortran中也没有无符号整数。

  type (c_ptr):: start,ref 
integer(c_int):: res

start = c_loc(my_struct%a)
ref = c_loc(my_struct%b%c)
$ b $ res = int(transfer(start,1_c_intptr_t) - transfer(ref,1_c_intptr_t),c_int)

如果指针值大于签名 c_intptr_t 的最大正值,则可能有问题。


I'd like to have a kind of diffptr_t in fortran with iso_c_bindings. The memory distance result must be a signed int.

type(c_ptr) :: start,ref
type(c_int) :: res
start=c_loc(my_struct%a)
ref=c_loc(my_struct%b%c)
res=start-ref

Compilation error:

This binary operation is invalid for this data type.
An arithmetic or LOGICAL type is required in this context.

Thanks

解决方案

You cannot do pointer arithmetic in standard Fortran. You have to rely on the processor dependent binary correspondence between pointers and integers.

Also, there are no unsigned integers in Fortran.

type(c_ptr) :: start,ref
integer(c_int) :: res

start = c_loc(my_struct%a)
ref = c_loc(my_struct%b%c)

res = int( transfer(start, 1_c_intptr_t) - transfer(ref, 1_c_intptr_t) , c_int)

There may be problems, if the pointer values are larger, than the maximum positive value for the signed c_intptr_t.

这篇关于diffptr_t fortran与iso_c_bindings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆