遗留代码中的动态数组与静态数组
我负责许多遗留 (F77) 程序。有时,其中一个或另一个可能会由于尝试超过固定数组大小而失败。我通常的解决方法是方式过度分配有问题的数组。
有没有人有关于将这些固定数组更改为动态数组的想法或经验,以及对代码的其余部分(包括子例程调用)有何影响?
我在 OpenVMS 上使用相当新的编译器,因此我相信不会出现编译器问题。
I'm responsible for a number of legacy (F77) programs. On occasion one or another may fail due to attempting to exceed a fixed array size. My usual fix is to way over allocate the offending array.
Does anyone have thoughts or experience on changing these fixed arrays to dynamic and what are the ramifications to the rest of the code including subroutine calls?
I'm using a fairly new compiler on OpenVMS so I believe that there will not be compiler issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能更清楚地了解一下您目前在做什么以及打算做什么吗?您声明您“过度分配”数组,这表明您已经在使用动态数组,然后在下一句中您询问有关将固定数组更改为动态数组的问题。
也许您的意思是您在编译时定义的数组的空间比您预期使用的空间多?这是 Fortran 程序员长期以来的工作方式之一。然而,从 Fortran 90 开始,该语言以标准方式支持动态数组,即那些大小在运行时确定的数组。关键字 ALLOCATABLE 用于声明此类数组,并通过 ALLOCATE 过程为它们分配空间(通常在堆上)。当然,此后数组大小是固定的。要动态扩展数组,通常必须分配一个更大的数组,然后复制元素。
如果您希望继续使用旧程序,那么我建议转换为可分配数组的努力将得到回报。自从 Fortran 90 编译器广泛使用以来,我(我怀疑大多数其他 Fortran 程序员)已经做了很多这样的事情。回报努力的方式之一是让您专注于维护的其他方面。考虑到现代计算机上内存的可用性,与上一代编写代码的人相比,您无需担心使用这些空间。我预计代码的用户也正在尝试解决比他们的祖先更大的问题。分配数组将提供某种面向未来的措施。
至于对代码其他部分的影响,请考虑一下:
我预计有很多事情我已经忘记了。
Can you be a bit clearer about what you currently do and what you propose to do ? You state that you 'way over allocate' arrays, which suggests that you are already using dynamic arrays, then in the next sentence you ask about changing fixed arrays to dynamic.
Perhaps you mean that you define the arrays, at compile time, with more space than you expect to use ? That's one of the ways Fortran programmers have worked for a long time. Since Fortran 90, however, the language has supported, in a standard way, dynamic arrays, ie those whose size is established at run time. The keyword ALLOCATABLE is used to declare such arrays, and they are given space (on the heap generally) with the ALLOCATE procedure. Of course, thereafter the array size is fixed. To dynamically expand an array you generally have to ALLOCATE a larger array, then copy elements across.
If you expect to continue to use your legacy programs then I suggest that the effort of converting to allocatable arrays will be repaid. This is something that I, and I suspect most other Fortran programmers, have done a lot of in the years since Fortran 90 compilers became widely available. One of the ways in which the effort will be repaid is in allowing you to concentrate on other aspects of maintenance. Given the availability of memory on modern computers you need be much less concerned about using such space than the people who wrote the code a generation ago. I expect that the users of the code are trying to tackle much bigger problems than their forefathers too. Allocating arrays will provide some measure of future-proofing.
As for the impact on other parts of the code, give some thought to:
I expect there's a bunch of stuff I've forgotten.