Dynamic-length items

Enterprise COBOL supports usage of dynamic-length items. Generally, dynamic-length items are items whose logical length might change at runtime.

Dynamic-length elementary items

A dynamic-length item is a data item whose logical length might change at runtime.

If a dynamic-length elementary item is used as a sending operand (which might be reference-modified), the item is treated as a fixed-length data item with a length that is the same as the current length of the dynamic-length elementary item. If a dynamic-length elementary item is used as a receiving operand and is not reference-modified, the sender contents are moved into the receiver's content buffer.

If the length of the sender is longer than the length of the receiver, then a larger receiver content buffer might be allocated at runtime to contain the contents of the sender, before moving any data. The length of the receiver is then set to the length of the sender. If the length of the sender is zero, no data is moved and the length of the receiver is set to zero. If the sender is a figurative constant, the length of the operand is as specified in Figurative constants.

If a dynamic-length elementary item is used as a receiving operand and is reference-modified, the item is treated as a fixed-length item with a length that is the same as the current length of the dynamic-length elementary item. The dynamic-length elementary item receiver buffer will not be allocated or reallocated if it is reference-modified.

Note: Do not use dynamic-length elementary items as reference-modified receiving items before they have been initialized. Doing so results in unpredictable behavior.

For items described with the DYNAMIC LENGTH clause, the number of bytes allocated for the item depends on the length of the item at program runtime.

Dynamic-length group items

A dynamic-length group item is a group item that contains a subordinate dynamic-length elementary item and whose logical length might change at runtime.

A dynamic-length group item's data description entry contains one or more subordinate dynamic-length elementary items. A group item that is not a dynamic-length group item is considered to be a fixed-length group item, such as group items that do not contain subordinate dynamic-length elementary items. Fixed-length group items can contain variable length tables whose data description entry contains the OCCURS DEPENDING ON clause.

Dynamic-length group items may not be compared to, or moved to, other group items (dynamic-length or not).

Fixed-length group items are always compatible and comparable with other fixed-length group items.