ALLOCATE Statement
The ALLOCATE
Statement allocates memory.
General Format:
ALLOCATE [{numeric-data-1 CHARACTERS}
{data-ptr-1 }
[ INITIALIZED ]
[ RETURNING data-ptr-2
Syntax:
numeric-data-1
is a numeric data item or literal with a positive integer value.data-ptr-n
is a data item declared withUSAGE POINTER
.
General Rules:
- If
data-ptr-1
is specified, it requires theBASED
clause. - When initialized, the address of the based data item referenced by
data-ptr-1
is set to the predefined addressNULL
. - If
data-ptr-1
is not specified,data-ptr-2
must be specified. - The allocated storage persists until explicitly released with a
FREE
statement or the run unit is terminated, whichever occurs first.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. ALLOCATE-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 DATA-PTR-1 USAGE POINTER BASED.
77 DATA-PTR-2 USAGE POINTER.
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
*ALLOCATE [{EXPR CHARACTERS}[INITIALIZED][RETURNING DATA-PTR-2]
* {DATA-PTR-1 }
ALLOCATE DATA-PTR-1 INITIALIZED.
ALLOCATE 100 CHARACTERS INITIALIZED RETURNING DATA-PTR-2.
DISPLAY "ALLOCATE-1 FINISHED!" LINE 10 COL 10.
FREE DATA-PTR-1, DATA-PTR-2.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.