ALLOCATABLE数组或指针数组? [英] ALLOCATABLE arrays or POINTER arrays?

查看:562
本文介绍了ALLOCATABLE数组或指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Fortran的一个新的code和使用分配数组或指针数组之间犹豫。我读的地方,分配数组超过指针数组有显著的优点:

I am writing a new code in Fortran and hesitating between using allocatable arrays or pointer arrays. I read somewhere that allocatable arrays have significant advantages over pointer arrays:

1)更有效,因为他们总是在内存中连续

1) More efficient because they are always contiguous in memory

2)没有内存泄漏是可能的。

2) No memory leaks are possible

有人可以证实这一点?哪一个你会建议使用?什么是这两个选择之间的code的执行速度的长期结果?

Can someone confirm this? Which one would you advise to use? What are the results in term of execution speed of the code between these two alternatives?

推荐答案

分配数组可能会导致更有效的code,因为该阵列将是连续的。特别是如果数组传递到子程序,邻接可以prevent需要编译器创建的临时副本。

Allocatable arrays can result in more efficient code because the arrays will be contiguous. Particularly if the array is passed to a subroutine, being contiguous can prevent the need for the compiler creating a temporary copy.

有关在子程序局部变量(不SAVE属性)(对于Fortran 95以上),可分配数组会自动在从子程序出口释放,避免了内存泄漏。内存泄漏是不可能的allocatables,除了在不解除分配一个数组,它是不再需要程序员的意义。

For local variables in subroutines (without the SAVE attribute) (for Fortran 95 and higher), allocatable arrays are automatically deallocated upon exit from the subroutine, avoiding a memory leak. Memory leaks aren't possible with allocatables, except in the sense of the programmer not deallocating an array that is no longer need.

使用指针,您可以重新分配的指针,留下一些记忆无法访问,迷路 - 泄漏的一种形式。如果可分配将做的工作,我推荐使用该方法,而不是一个指针。

With pointers, you can reassign a pointer, leaving some memory inaccessible and lost -- one form of a leak. If an allocatable will do the job, I recommend using that method instead of a pointer.

某些理由使用指针:服用阵列,或创建数据结构的一个部分,如一个链表。为了创建在运行时确定大小的数组的目的,我会使用可分配的。

Some reasons to use pointers: taking a section of an array, or creating a data structure such as a linked list. For the purpose of creating an array of size determined at run time, I'd use an allocatable.

这篇关于ALLOCATABLE数组或指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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