The first request for dynamic storage within an activation group results in the creation of a default heap from which the storage allocation takes place. Additional requests for dynamic storage are met by further allocations from the default heap. If there is insufficient storage in the heap to satisfy the current request for dynamic storage, the heap is extended, and the additional storage is allocated.
Allocated dynamic storage remains allocated until it is explicitly freed, or until the heap is discarded. The default heap is discarded only when the owning activation group ends.
Programs in the same activation group all use the same default heap. If one program accesses storage beyond what has been allocated, it can cause problems for another program.
For example, assume that two programs, PGM1 and PGM2 are running in the same activation group. 10 bytes are allocated for PGM1, but 11 bytes are changed by it. If the extra byte was in fact allocated for PGM2 problems may arise for PGM2.
You can use the following ILE bindable APIs on the default heap:
In an ILE C++ program, you manage dynamic storage belonging to the default heap using the operators new and delete to create and delete dynamic objects. Dynamic objects are never created and deleted automatically. Their creation can fail if there is not enough free heap space available, and your programs must provide for this possibility.
The following figures illustrate how to use the new and delete operators for dynamic storage allocation:
Figure 37. Example of Dynamic Allocation and De-Allocation of Storage for a Class Object
|
Figure 38. Example of Dynamic Allocation and De-Allocation of Storage for an Array of Objects
TClass *array; // Define pointer array = new TClass[100];// Construct array of 100 objects ... delete[] array; // Delete array
|
The C++ standard allows an application to redefine a number of
replacement functions. The program's definitions are
used instead of the default versions supplied by the library. Such
replacement occurs prior to program startup.
A C++ program may provide the definition for any of the eight dynamic memory allocation functions. These include:
When overriding replacement functions, consider the following limitations:
The ISO C++ Standard categorizes operator new and operator
delete as replacement functions, which means that they can be
redefined in a C++ program. However, the standard allows only one
definition of an operator to be in effect during program execution.
For detailed information about visibility issues, see WebSphere Development Studio: ILE C/C++ Language Reference.
Example:
Suppose an application uses three C++ source files (one.cpp, two.cpp, and three.cpp):
After you compile the application, the redefined operator new (or operator delete) is visible to, and used for, all translation units.
Given the same three-translation unit scenario, suppose that one.cpp is compiled with the C compiler. The redefined operator is visible in translation unit two.cpp but not in three.cpp. Any calls to operator new (or operator delete) outside of translation unit two.cpp uses the standard version, not the user-defined version, of the operator.
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.