GETMAIN
GETMAIN
The CICS GETMAIN statement is used to allocate (reserve) a block of dynamic storage (memory) for use during the execution of a transaction. This allocation is necessary when a program requires additional memory for various purposes, such as:
- Holding large datasets.
- Creating dynamic working storage areas.
- Managing variable-length data.
- Handling communication buffers.
Once storage has been allocated using GETMAIN, it is important to explicitly release it using the FREEMAIN command to prevent memory leaks.
Syntax -
EXEC CICS GETMAIN
SET(pointer-variable)
LENGTH(data-value)
[INITIMG(initial-value)]
[FLENGTH(field-length-variable)]
[SHARED]
[RESP(response-field)]
[RESP2(response-field2)]
END-EXEC.
- SET(pointer-variable) - Specifies the pointer variable that will reference the allocated memory.
- LENGTH(data-value) - Defines the size (in bytes) of the storage to be allocated.
- INITIMG(initial-value) - Optional. Fills the allocated memory with a specified initial character.
- FLENGTH(field-length-variable) - Optional. Stores the actual allocated storage size.
- SHARED - Allocates memory that can be shared between transactions.
- RESP(response-variable) - Optional. It captures the response code of the GETMAIN operation and used to check if the command executed successfully or encountered an error.
- RESP2(response2-variable) - Optional. It captures the response2 code of the GETMAIN operation when the error occured.
Short Examples -
Scenario - Allocating Storage and Initializing Memory
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STORAGE-LENGTH PIC S9(4) COMP VALUE 100.
01 WS-RESP PIC S9(4) COMP.
PROCEDURE DIVISION.
EXEC CICS GETMAIN
SET(WS-POINTER)
LENGTH(WS-STORAGE-LENGTH)
INITIMG(' ')
RESP(WS-RESP)
END-EXEC.
IF WS-RESP = DFHRESP(NORMAL) THEN
...
ELSE
...
END-IF.
...
...
EXEC CICS FREEMAIN
POINTER(WS-POINTER)
RESP(WS-RESP)
END-EXEC.
...
GETMAIN is used to allocate 100 bytes of memory. The memory is initialized with spaces (INITIMG(' ')). The response code (RESP) is checked to ensure successful allocation. The allocated memory is released using FREEMAIN to avoid memory leaks.
Error Conditions -
Eror Condition | RESP | RESP2 | Reason |
---|---|---|---|
LENGERR | 22 | 1 | The FLENGTH value is less than 1 or greater than the length of the target dynamic storage area. |
NOSTG | 42 | 2 | The storage requested is more than is currently available in the target DSA. |