PrevNextUpHome SophiaFramework UNIVERSE 5.3

14.2. Heap Class

Heap class is the class that encapsulate the MALLOC and FREE functions.

There are the SFXHeap class and the SFXClusterHeap class that supports block allocation.

These classes subsitute the MALLOC (SFXHelper::malloc), REALLOC (SFXHelper::realloc), and FREE ( SFXHelper::free) functions.

Example 14.1. Define the heap

// heap class used as array of SInt16 type.
SFXHeap<SInt16> heap;

// to use block allocation, write: SFXClusterHeap<SInt16> heap; 
// how to use SFXClusterHeap is similar to SFXHeap

Example 14.2. Allocate the Heap

// reserve 256-byte of heap like MALLOC (MemoryAllocate) function
heap.Resize(sizeof(SInt16) * 128);

Example 14.3. Set the values

heap[0] = 100;
heap[1] = 200;
heap[2] = 300;
heap[3] = 400;

Example 14.4. Get the value from the heap

SInt16 m = heap[2]; // m = 300

Example 14.5. Get the heap size

SInt16 n = heap.GetSize(); // n = 256

Example 14.6. Resize the heap

// like REALLOC (MemoryReallocate) function

Example 14.7. Get the pointer to the heap

// get pointer to heap, and then use it
SInt16Ptr p = static_cast<SInt16Ptr>(heap.GetHeap());

Example 14.8. Release memory after using the heap

// no need to release allocated memory since it is automatically released

// or release allocated memory explicitly
 heap.Free();  // FREE ( MemoryFree )