Note: This clause is only supported for native COBOL code.
The DYNAMIC LENGTH clause describes a dynamic-length elementary item.

General Format

The Dynamic Length clause

Syntax Rules

  1. Dynamic-length elementary items must be specified in the WORKING-STORAGE SECTION or the LOCAL-STORAGE SECTION.
  2. Dynamic-length elementary items must have a PICTURE clause of either PIC X or PIC U, or be defined with a single X or U; not other definitions are permitted.
  3. When the LIMIT phrase is used, truncation at a character boundary on the right occurs if an attempt is made to receive into a dynamic-length elementary item that is not sufficient in size.
  4. If the LIMIT phrase is not specified, the maximum value for the data item class is the assumed limit, unless the value is limited by available run-time memory or other run-time environment limits.
  5. integer-1 specifies the maximum number of bytes that the item can contain, and must be set to an integer greater than or equal to 1.
  6. The DYNAMIC LENGTH clause cannot be used in conjunction with the BYTE-LENGTH clause.
  7. DYNAMIC LENGTH items or groups cannot have a REDEFINES clause or be redefined.

General Rules

  1. A dynamic-length elementary item is an elementary data item whose data declaration entry contains the DYNAMIC LENGTH clause, and which may vary in length at run time. It has a minimum length of zero, and a maximum length of either the value of integer-1 or the available run-time memory (whichever is smallest).
  2. Groups with subordinate dynamic-length elementary items are referred to as dynamic-length groups.
  3. Dynamic-length elementary items cannot be variably located or a subordinate data item within a table containing the OCCURS DEPENDING ON phrase. If a group contains a dynamic-length elementary item, the content of the item can be considered logically inline within this group, even if the physical location of the content is remotely located.
  4. Comparisons and moves between groups where one or both groups contains a subordinate dynamic-length elementary item are not permitted.
  5. When using a dynamic-length elementary item as a sending operand (including one reference-modified), the item is handled as a fixed-length data item equivalent to the current length of the dynamic-length elementary item. When used as a receiving operand, with no reference modification, the sending item's contents are moved into the receiving item's content buffer.

    When the length of a sending item is longer than the length of a dynamic-length receiving item that is not reference modified, a larger receiving content buffer could be allocated at run time, to hold the contents of the sending item before any data is moved. The length of the receiving item is then set to the length of the sending item. When the length of the sending item is zero, no data is moved and the length of the receiving item is set to zero. If the sending item is a figurative constant, the length of the operand is the length of a single value of the constant.

    When using a dynamic-length elementary item that is reference modified as a receiving item, the item is handled as a fixed-length item equivalent to the current length of the dynamic-length elementary item. The receiving buffer of the dynamic-length elementary item will not be allocated or reallocated if it is reference-modified.

  6. The following statements support dynamic-length elementary items: ACCEPT (partial support), CALL, CANCEL, DISPLAY, EVALUATE, IF, MOVE, SET, STOP, STRING, UNSTRING.