遗留代码中的动态数组与静态数组

发布于 2024-09-24 09:11:35 字数 200 浏览 2 评论 0原文

我负责许多遗留 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2024-10-01 09:11:35

您能更清楚地了解一下您目前在做什么以及打算做什么吗?您声明您“过度分配”数组,这表明您已经在使用动态数组,然后在下一句中您询问有关将固定数组更改为动态数组的问题。

也许您的意思是您在编译时定义的数组的空间比您预期使用的空间多?这是 Fortran 程序员长期以来的工作方式之一。然而,从 Fortran 90 开始,该语言以标准方式支持动态数组,即那些大小在运行时确定的数组。关键字 ALLOCATABLE 用于声明此类数组,并通过 ALLOCATE 过程为它们分配空间(通常在堆上)。当然,此后数组大小是固定的。要动态扩展数组,通常必须分配一个更大的数组,然后复制元素。

如果您希望继续使用旧程序,那么我建议转换为可分配数组的努力将得到回报。自从 Fortran 90 编译器广泛使用以来,我(我怀疑大多数其他 Fortran 程序员)已经做了很多这样的事情。回报努力的方式之一是让您专注于维护的其他方面。考虑到现代计算机上内存的可用性,与上一代编写代码的人相比,您无需担心使用这些空间。我预计代码的用户也正在尝试解决比他们的祖先更大的问题。分配数组将提供某种面向未来的措施。

至于对代码其他部分的影响,请考虑一下:

  • 使用完数组后进行 DEALLOCATE。
  • 在子例程和函数内部分配的数组会在退出时自动释放,除非您将数组返回到调用单元——但这只有在 Fortran 2003 及更高版本中才可能(根据标准,您的编译器可能有所不同)。
  • 如果您分配了一个数组,您可以将其传递到子程序中,也可以将其从子程序中取出,就像处理任何其他数组一样。
  • 在 FORTRAN77 中,将数组的维度传递到带有数组的子程序中是标准做法;在 Fortran 90 及更高版本中,您不必这样做,如果您需要知道大小,则可以使用 SIZE 内在函数。使用整个数组语法,您通常不需要知道数组的大小。
  • 您可能会发现围绕它编写包装器更容易,而不是重写大量数组处理代码。
  • 作为良好实践,您应该检查 ALLOCATE 语句的 stat 选项的值。

我预计有很多事情我已经忘记了。

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:

  • DEALLOCATEing when you are finished with the array.
  • Arrays allocated inside subroutines and functions are automatically deallocated on exit, unless you return the array to the calling unit -- but this is only possible (according to the standard, your compiler may differ) in Fortran 2003 and later.
  • If you ALLOCATE an array you can pass it in to, and get it out of, a subprogram just as you can any other array.
  • In FORTRAN77 it was standard practice to pass the dimensions of an array into a sub-program with the array; in Fortran 90 and later you don't have to do this, if you need to know the size there is the SIZE intrinsic. And with whole-array syntax you often don't need to know what size an array is anyway.
  • Rather than rewrite a lot of your array processing code you might find it easier to write wrappers around it.
  • As a matter of good practice you should check the value of the stat option to the ALLOCATE statement.

I expect there's a bunch of stuff I've forgotten.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文