可变长度表中的内存分配
假设我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有提及您正在使用哪个编译器,但是,至少在当前的 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.
在 CICS 世界中,OCCURS DEPENDING ON (ODO) 可用于创建
在运行时动态调整大小的表。但是,您声明的方式
SOAP-RECORD
将分配足够的内存来保存最大大小的记录。请尝试以下操作:
首先,将
SOAP-RECORD
移至LINKAGE SECTION
中。申报物品在链接部分没有为它们分配任何内存。在此
点你只有一个记录布局。留下声明
WORKING-STORAGE
中的ITEM-COUNT
和SUB-COUNT
。接下来,在 WORKING-STORAGE 中声明一个指针和长度,如下所示:
最后在 PROCEDURE DIVISION 中:设置数组的大小
一些实际值的尺寸;分配
适当数量的内存,然后将两者连接起来。例如:
这将仅分配足够的内存来存储包含 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
intoLINKAGE SECTION
. Items declaredin 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
andSUB-COUNT
inWORKING-STORAGE
.Next, declare a pointer and a length in
WORKING-STORAGE
something like:Finally in the
PROCEDURE DIVISION
: Set the size of the arraydimensions to some real values; allocate the
appropriate amount of memory and then connect the two. For example:
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 ofSOAP-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!