action.skip

ALLOCATE Statement

The ALLOCATE Statement allocates memory.

General Format:

       ALLOCATE [{numeric-data-1 CHARACTERS} 
                 {data-ptr-1               } 
                [ INITIALIZED ] 
                [ RETURNING data-ptr-2

Syntax:

  1. numeric-data-1 is a numeric data item or literal with a positive integer value.
  2. data-ptr-n is a data item declared with USAGE POINTER.

General Rules:

  1. If data-ptr-1 is specified, it requires the BASED clause.
  2. When initialized, the address of the based data item referenced by data-ptr-1 is set to the predefined address NULL.
  3. If data-ptr-1 is not specified, data-ptr-2 must be specified.
  4. 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.