可变长度表中的内存分配

发布于 2024-08-17 10:08:27 字数 1064 浏览 4 评论 0原文

假设我在 WORKING-STORAGE 中定义了以下可变长度表...

01  SOAP-RECORD.                                           
    05  SOAP-INPUT        PIC X(8)          VALUE SPACES.
    05  SOAP-STATUS       PIC 9             VALUE ZERO.                         
    05  SOAP-MESSAGE      PIC X(50)         VALUE SPACES.
    05  SOAP-ITEMS        OCCURS 0 TO 500 TIMES   
                          DEPENDING ON ITEM-COUNT
                          INDEXED BY ITEM-X.     
        10 SI-SUB-ITEMS   OCCURS 0 TO 100 TIMES
                          DEPENDING ON SUB-COUNT
                          INDEXED BY SUB-X.     
           15 SS-KEY      PIC X(8)          VALUE SPACES.
           15 SS-AMOUNT   PIC -9(7).99      VALUE ZEROS.
           15 SS-DESCR    PIC x(100)        VALUE SPACES.

当该程序运行时,它最初会分配该表可能需要的空间,还是分配更加动态记忆?我猜想 DEPENDING ON 子句会使其更加动态,因为随着 ITEM-COUNT 变量的增加,它会分配更多内存。一位同事告诉我情况并非如此,但他并不能 100% 确定。所以我真的很想知道它是如何工作的,以便尽可能有效地构建我的程序。

PS:是的,我正在编写一个新的 COBOL 程序!它实际上是一个 CICS Web 服务。我不认为这种语言会消亡:(

Say I have the following variable-length table defined in WORKING-STORAGE...

01  SOAP-RECORD.                                           
    05  SOAP-INPUT        PIC X(8)          VALUE SPACES.
    05  SOAP-STATUS       PIC 9             VALUE ZERO.                         
    05  SOAP-MESSAGE      PIC X(50)         VALUE SPACES.
    05  SOAP-ITEMS        OCCURS 0 TO 500 TIMES   
                          DEPENDING ON ITEM-COUNT
                          INDEXED BY ITEM-X.     
        10 SI-SUB-ITEMS   OCCURS 0 TO 100 TIMES
                          DEPENDING ON SUB-COUNT
                          INDEXED BY SUB-X.     
           15 SS-KEY      PIC X(8)          VALUE SPACES.
           15 SS-AMOUNT   PIC -9(7).99      VALUE ZEROS.
           15 SS-DESCR    PIC x(100)        VALUE SPACES.

When this program runs, will it initially allocate as much space as this table could possibly need, or is it more dynamic about allocating memory? I would guess that the DEPENDING ON clause would make it more dynamic in the sense that it would allocate more memory as the ITEM-COUNT variable is incremented. A co-worker tells me otherwise, but he is not 100% sure. So I would really like to know how this works in order to structure my program as efficiently as possible.

PS: Yes, I am writing a new COBOL program! It's actually a CICS web service. I don't think this language will ever die :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清风不识月 2024-08-24 10:08:27

您没有提及您正在使用哪个编译器,但是,至少在当前的 2002 COBOL 标准之前,为 OCCURS...DEPENDING ON (ODO) 数据项分配的空间不需要是动态的。 (实际上,数据项的变化只是出现的次数,而不是长度。)尽管您的编译器供应商可能已经实现了标准的扩展,但我不知道有任何供应商在该领域这样做了。

该标准的下一个(但尚未批准)修订包括对具有新 OCCURS DYNAMIC 格式的动态容量表的支持。

You don't mention which compiler you're using, but, at least up through the current, 2002, COBOL standard, the space allocated for an OCCURS...DEPENDING ON (ODO) data item is not required to be dynamic. (It's really only the number of occurrences, not the length, of the data item that varies.) Although your compiler vendor may've implemented an extension to the standard, I'm not aware of any vendor that has done so in this area.

The next, but not yet approved, revision of the standard includes support for dynamic-capacity tables with a new OCCURS DYNAMIC format.

别靠近我心 2024-08-24 10:08:27

在 CICS 世界中,OCCURS DEPENDING ON (ODO) 可用于创建
在运行时动态调整大小的表。但是,您声明的方式
SOAP-RECORD 将分配足够的内存来保存最大大小的记录。

请尝试以下操作:

首先,将 SOAP-RECORD 移至 LINKAGE SECTION 中。申报物品
在链接部分没有为它们分配任何内存。在此
点你只有一个记录布局。留下声明
WORKING-STORAGE 中的 ITEM-COUNTSUB-COUNT

接下来,在 WORKING-STORAGE 中声明一个指针和长度,如下所示:

77 SOAP-PTR       USAGE POINTER.
77 SOAP-LENGTH    PIC S9(8) BINARY.

最后在 PROCEDURE DIVISION 中:设置数组的大小
一些实际值的尺寸;分配
适当数量的内存,然后将两者连接起来。例如:

MOVE 200 TO ITEM-COUNT
MOVE 15 TO SUB-COUNT
MOVE LENGTH OF SOAP-RECORD TO SOAP-LENGTH
EXEC CICS GETMAIN
     BELOW
     USERDATAKEY
     SET(SOAP-PTR)
     FLENGTH(SOAP-LENGTH)
END-EXEC
SET ADDRESS OF SOAP-RECORD TO SOAP-PTR

这将仅分配足够的内存来存储包含 200 个 SOAP-ITEMS 的 SOAP-RECORD
每个包含 15 个 SI-SUB-ITEMS。

请注意,LENGTH OF 寄存器为您提供了 SOAP-RECORD 的大小
基于 ODO 对象值(ITEM-COUNT、SUB-COUNT)而不是
发生的最大数量。

非常重要...完成后不要忘记释放内存!

In the CICS world, OCCURS DEPENDING ON (ODO) can be used to create a
table that is dynamically sized at run time. However, the way you are declaring
SOAP-RECORD will allocate enough memory to hold a record of maximum size.

Try the following:

First, move the SOAP-RECORD into LINKAGE SECTION. Items declared
in the linkage section do not have any memory allocated for them. At this
point you only have a record layout. Leave the declaration of
ITEM-COUNT and SUB-COUNT in WORKING-STORAGE.

Next, declare a pointer and a length in WORKING-STORAGE something like:

77 SOAP-PTR       USAGE POINTER.
77 SOAP-LENGTH    PIC S9(8) BINARY.

Finally in the PROCEDURE DIVISION: Set the size of the array
dimensions to some real values; allocate the
appropriate amount of memory and then connect the two. For example:

MOVE 200 TO ITEM-COUNT
MOVE 15 TO SUB-COUNT
MOVE LENGTH OF SOAP-RECORD TO SOAP-LENGTH
EXEC CICS GETMAIN
     BELOW
     USERDATAKEY
     SET(SOAP-PTR)
     FLENGTH(SOAP-LENGTH)
END-EXEC
SET ADDRESS OF SOAP-RECORD TO SOAP-PTR

This will allocate only enough memory to store a SOAP-RECORD with 200 SOAP-ITEMS
each of which contain 15 SI-SUB-ITEMS.

Note that the LENGTH OF register gives you the size of SOAP-RECORD
based on the ODO object values (ITEM-COUNT, SUB-COUNT) as opposed to
the maximum number of OCCURS.

Very important... Don't forget to deallocate the memory when your done!

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