ILE C/C++ Programmer's Guide

Managing the Default Heap

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.

Using Bindable APIs to Manage the Default Heap

You can use the following ILE bindable APIs on the default heap:

Free Storage (CEEFRST)
Frees one previous allocation of heap storage

Get Heap Storage (CEEGTST)
Allocates storage within a heap

Reallocate Storage (CEECZST)
Changes the size of previously allocated storage

Dynamically Allocating Storage at Run Time



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



TClass *p; // Define pointer
p= new TClass; // Construct object
if (!p) {
Error("Unable to construct object");
exit(1);
}
...
delete p; // Delete 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
Note:
In this example, you use delete[] to delete the
array. Without the brackets, delete deletes the entire
array, but calls the destructor only for the first element in the
array. If you have an array of values that do not have destructors, you
can use delete or delete[].

Overriding Replacement Functions



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:

Limitations

When overriding replacement functions, consider the following limitations:

Overloading the new or delete Operator

C++ language only 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.

Note:
Visibility issues can arise if a program does both of the following:

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.

Note:
Because user-defined and standard operators have different signatures, no binder error or compiler warning is generated.


[ Top of Page | Previous Page | Next Page | Table of Contents ]