Fortran指针分配,"="之间的区别在于:和"=" [英] Fortran pointer assignment, difference between "=>" and "="

查看:62
本文介绍了Fortran指针分配,"="之间的区别在于:和"="的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fortran 95中将一个指针分配给另一个指针时,我正在努力理解 => = 的不同行为.说我有一个派生数据类型 foo ,那么以下代码段的最后两行之间有什么区别,它们等效吗?

I am struggling to understand the different behavior of => and = when assigning one pointer to another in Fortran 95. I.e. say I have a derived data type foo, what then is the difference between the last two lines in the following snippet, are they equivalent?

type(foo), target :: f
type(foo), pointer :: p1, p2

f = foo(...)
p1 => foo

p2 => p1
p2 = p1

推荐答案

不,它们绝对不等效,误认它们会带来很大的后果(例如

No, they are absolutely not equivalent and mistaking them has large consequences (like Fortran Functions with a pointer result in a normal assignment).

= 值赋值(或者如果您想精确的话,就是赋值),它将获取存储在右侧的值并将其复制到左手边如果左侧是指针,它将把值复制到指针的目标.如果指针没有指向任何地方(空)或指向某个未定义的位置(垃圾地址),则这是未定义的行为,但很可能发生崩溃.

= is the assignment of value (or just assignment if you want to be exact), it will take the value stored on the right hand side and copy it to the left hand side. If the left hand side is a pointer, it will copy the value to the target of the pointer. If the pointer did not point anywhere (null) or to some undefined place (a garbage address), it is an undefined behaviour but a crash is very likely.

=> 指针分配,左侧必须是指针,右侧必须是目标或指针.左侧的指针将指向右侧的目标(或指针的目标).如果左侧的指针已经指向某个内存,该指针先前是由 allocate 语句通过指针分配的,并且它是唯一指向该内存的指针,则该内存将丢失(内存泄漏)).

=> is the pointer assignment, the left hand side must be a pointer, the right hand side must be a target or a pointer. The pointer on the left hand side will point to the target (or the target of the pointer) on the right hand side. If the pointer on the left was already pointing to some memory, that was previously allocated by an allocate statement through a pointer, and it was the only pointer pointing there, that memory will be lost (a memory leak).

这篇关于Fortran指针分配,"="之间的区别在于:和"="的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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