Dynamic memory allocation in COBOL allows programs to allocate and free memory at runtime rather than having all memory pre-allocated at compile time. This enables more efficient memory usage, handling of variable-sized data structures, and memory management based on runtime conditions. COBOL provides the ALLOCATE and FREE statements, along with pointers and BASED storage, to support dynamic memory management.
This comprehensive guide will teach you how to use ALLOCATE to dynamically allocate memory, FREE to release it, work with pointers to reference allocated memory, use BASED storage for dynamic structures, and follow best practices for efficient memory management. Understanding dynamic memory is essential for modern COBOL programming, especially when dealing with variable-length data, dynamic arrays, and memory-efficient processing.
Dynamic memory is memory that is allocated and freed at runtime, as opposed to static memory which is allocated at compile time. Dynamic memory provides:
COBOL supports dynamic memory through:
Understanding the difference between static and dynamic memory:
| Aspect | Static Memory | Dynamic Memory |
|---|---|---|
| Allocation Time | Compile time (when program is compiled) | Runtime (when ALLOCATE executes) |
| Size | Fixed size determined at compile time | Variable size determined at runtime |
| Location | Fixed location in memory | Location determined at runtime |
| Lifetime | Entire program execution | From ALLOCATE until FREE |
| Management | Automatic, no explicit management needed | Explicit ALLOCATE and FREE required |
| Use Cases | Fixed-size, always-needed data | Variable-size or conditionally-needed data |
| Example | WORKING-STORAGE data items | ALLOCATE ... RETURNING pointer |
Pointers in COBOL are data items that contain memory addresses. They're used to reference dynamically allocated memory. Pointers are defined with USAGE IS POINTER.
1234WORKING-STORAGE SECTION. 01 RECORD-POINTER USAGE IS POINTER. 01 DATA-POINTER USAGE IS POINTER. 01 NULL-POINTER USAGE IS POINTER VALUE NULL.
Pointer characteristics:
The ALLOCATE statement dynamically allocates memory for data items at runtime.
1ALLOCATE identifier-1 [INITIALIZED] [RETURNING pointer-name]
Components:
1234567891011121314151617181920212223242526WORKING-STORAGE SECTION. 01 DYNAMIC-RECORD. 05 CUSTOMER-ID PIC 9(8). 05 CUSTOMER-NAME PIC X(30). 05 ACCOUNT-BALANCE PIC 9(8)V99. 01 RECORD-PTR USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Allocate memory for dynamic record ALLOCATE DYNAMIC-RECORD RETURNING RECORD-PTR *> Use allocated memory through pointer SET ADDRESS OF DYNAMIC-RECORD TO RECORD-PTR MOVE 12345678 TO CUSTOMER-ID MOVE 'JOHN SMITH' TO CUSTOMER-NAME MOVE 1000.00 TO ACCOUNT-BALANCE DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Balance: $" ACCOUNT-BALANCE *> Free allocated memory FREE RECORD-PTR STOP RUN.
This example allocates memory for a record, uses it, and then frees it. The pointer is used to reference the allocated memory.
123456789101112131415161718WORKING-STORAGE SECTION. 01 INITIALIZED-RECORD. 05 FIELD-1 PIC 9(5) VALUE 0. 05 FIELD-2 PIC X(10) VALUE SPACES. 01 RECORD-PTR USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Allocate and initialize memory ALLOCATE INITIALIZED-RECORD RETURNING RECORD-PTR *> Memory is initialized to VALUE clauses SET ADDRESS OF INITIALIZED-RECORD TO RECORD-PTR DISPLAY "Field 1: " FIELD-1 *> Displays 0 DISPLAY "Field 2: [" FIELD-2 "]" *> Displays spaces FREE RECORD-PTR STOP RUN.
INITIALIZED causes the allocated memory to be initialized to the values specified in VALUE clauses.
The FREE statement releases dynamically allocated memory back to the system.
1FREE pointer-name [identifier]
Components:
123456789101112131415161718192021WORKING-STORAGE SECTION. 01 DYNAMIC-DATA PIC X(100). 01 DATA-PTR USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Allocate memory ALLOCATE DYNAMIC-DATA RETURNING DATA-PTR *> Use allocated memory SET ADDRESS OF DYNAMIC-DATA TO DATA-PTR MOVE "Hello, World!" TO DYNAMIC-DATA DISPLAY DYNAMIC-DATA *> Free allocated memory FREE DATA-PTR *> After FREE, the memory is no longer accessible *> Attempting to use DATA-PTR is undefined STOP RUN.
Always FREE allocated memory when it's no longer needed to prevent memory leaks. After FREE, the memory is no longer accessible through the pointer.
BASED storage allows data items to be stored at a location specified by a pointer rather than at a fixed location. BASED data items are accessed through their associated pointer.
123456WORKING-STORAGE SECTION. 01 BASE-POINTER USAGE IS POINTER. 01 BASED-RECORD BASED. 05 RECORD-ID PIC 9(8). 05 RECORD-NAME PIC X(30). 05 RECORD-VALUE PIC 9(8)V99.
BASED characteristics:
12345678910111213141516171819202122232425262728WORKING-STORAGE SECTION. 01 RECORD-PTR USAGE IS POINTER. 01 DYNAMIC-RECORD BASED. 05 CUSTOMER-ID PIC 9(8). 05 CUSTOMER-NAME PIC X(30). 05 ACCOUNT-BALANCE PIC 9(8)V99. PROCEDURE DIVISION. MAIN-LOGIC. *> Allocate memory ALLOCATE DYNAMIC-RECORD RETURNING RECORD-PTR *> Set pointer to access BASED record SET ADDRESS OF DYNAMIC-RECORD TO RECORD-PTR *> Use BASED record MOVE 12345678 TO CUSTOMER-ID MOVE 'JOHN SMITH' TO CUSTOMER-NAME MOVE 1000.00 TO ACCOUNT-BALANCE DISPLAY "ID: " CUSTOMER-ID DISPLAY "Name: " CUSTOMER-NAME DISPLAY "Balance: $" ACCOUNT-BALANCE *> Free memory FREE RECORD-PTR STOP RUN.
BASED storage allows the same data structure to be used with different memory locations, enabling dynamic memory management.
Here's a complete example demonstrating dynamic memory allocation and management:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980IDENTIFICATION DIVISION. PROGRAM-ID. DYNAMIC-MEMORY-EXAMPLE. AUTHOR. MainframeMaster Tutorial. REMARKS. Complete example demonstrating dynamic memory allocation. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-PTR USAGE IS POINTER VALUE NULL. 01 CUSTOMER-COUNT PIC 9(4) VALUE 0. 01 ALLOCATION-FLAG PIC X(1) VALUE 'N'. 88 MEMORY-ALLOCATED VALUE 'Y'. 88 MEMORY-NOT-ALLOCATED VALUE 'N'. 01 CUSTOMER-RECORD BASED. 05 CUST-ID PIC 9(8). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC 9(8)V99. 05 CUST-STATUS PIC X(1). PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Dynamic Memory Management Example ===" DISPLAY " " PERFORM ALLOCATE-CUSTOMER-RECORD IF MEMORY-ALLOCATED PERFORM POPULATE-CUSTOMER-DATA PERFORM DISPLAY-CUSTOMER-DATA PERFORM FREE-CUSTOMER-RECORD END-IF DISPLAY " " DISPLAY "Dynamic memory example completed" STOP RUN. ALLOCATE-CUSTOMER-RECORD. DISPLAY "Allocating memory for customer record..." *> Allocate memory with initialization ALLOCATE INITIALIZED CUSTOMER-RECORD RETURNING CUSTOMER-PTR IF CUSTOMER-PTR NOT = NULL MOVE 'Y' TO ALLOCATION-FLAG SET ADDRESS OF CUSTOMER-RECORD TO CUSTOMER-PTR DISPLAY "Memory allocated successfully" ELSE DISPLAY "ERROR: Memory allocation failed" MOVE 'N' TO ALLOCATION-FLAG END-IF. POPULATE-CUSTOMER-DATA. DISPLAY "Populating customer data..." MOVE 12345678 TO CUST-ID MOVE 'JOHN SMITH' TO CUST-NAME MOVE 5000.00 TO CUST-BALANCE MOVE 'A' TO CUST-STATUS DISPLAY "Customer data populated". DISPLAY-CUSTOMER-DATA. DISPLAY " " DISPLAY "Customer Information:" DISPLAY " ID: " CUST-ID DISPLAY " Name: " CUST-NAME DISPLAY " Balance: $" CUST-BALANCE DISPLAY " Status: " CUST-STATUS. FREE-CUSTOMER-RECORD. DISPLAY " " DISPLAY "Freeing allocated memory..." IF CUSTOMER-PTR NOT = NULL FREE CUSTOMER-PTR MOVE NULL TO CUSTOMER-PTR MOVE 'N' TO ALLOCATION-FLAG DISPLAY "Memory freed successfully" ELSE DISPLAY "WARNING: Attempting to free NULL pointer" END-IF.
This complete example demonstrates allocating memory, using it, and freeing it. It includes error checking and proper memory management practices.
Memory leaks occur when dynamically allocated memory is not freed, causing memory to be unavailable for reuse. Follow these best practices to prevent memory leaks and manage memory efficiently:
12345ALLOCATE DATA-ITEM RETURNING DATA-PTR *> Use allocated memory SET ADDRESS OF DATA-ITEM TO DATA-PTR *> ... use DATA-ITEM ... FREE DATA-PTR
1234567IF CONDITION-MET ALLOCATE DATA-ITEM RETURNING DATA-PTR *> Use allocated memory SET ADDRESS OF DATA-ITEM TO DATA-PTR *> ... process ... FREE DATA-PTR END-IF
123456ALLOCATE RECORD-1 RETURNING PTR-1 ALLOCATE RECORD-2 RETURNING PTR-2 *> Use both records *> ... process ... FREE PTR-1 FREE PTR-2
12345ALLOCATE BASED-RECORD RETURNING RECORD-PTR SET ADDRESS OF BASED-RECORD TO RECORD-PTR *> Use BASED-RECORD *> ... process ... FREE RECORD-PTR
Think of dynamic memory like borrowing books from a library:
So dynamic memory is like a library system for your program - you get memory when you need it, use it, and return it when you're done!
Complete these exercises to reinforce your understanding:
Create a program that allocates memory for a customer record, populates it with data, displays it, and then frees the memory.
Create a program that conditionally allocates memory based on user input. If the user enters "Y", allocate and use memory; if "N", skip allocation. Always free memory if it was allocated.
Create a program that allocates memory for three different records, populates them, displays them, and frees all three allocations.
Create a program that uses BASED storage with a pointer. Allocate memory, set the pointer, use the BASED record, and free the memory.
Create a program that allocates memory, checks for successful allocation, handles allocation failures gracefully, and ensures memory is freed even if errors occur.
1. What statement is used to dynamically allocate memory in COBOL?
2. What statement is used to release dynamically allocated memory?
3. What is a pointer in COBOL?
4. What is BASED storage in COBOL?
5. What happens if you don't FREE allocated memory?
6. How do you access dynamically allocated memory?
7. What is the difference between static and dynamic memory?
8. When should you use dynamic memory allocation?