User manual

Table Of Contents
362
mikoC PRO for PIC32
MikroElektronika
Memory Manager Library
This library provides routines for manipulating dynamic memory allocation. Dynamic memory allocation (also known
as heap-based memory allocation) is the allocation of memory storage for use in a program during the runtime of that
program.
Dynamically allocated memory exists until it is released. This is in contrast to static memory allocation, which has a
xed duration. It is said that an object so allocated has a dynamic lifetime.
The heap memory size can be congured in the Edit Project window. Also, user can override heap memory size in the
code, by setting the HEAP_SIZE constant.
Library Routines
- Heap_Init
- malloc
- free
- LargestFreeMemBlock
- TotalFreeMemSize
Heap_Init
Prototype
void Heap_Init();
Description Sets Heap size.
Parameters None.
Returns Nothing.
Requires Nothing.
Example
const HEAP_SIZE = 3000; // declare Heap size
Heap_Init(); // set Heap size
Notes None.
malloc
Prototype
void *malloc(unsigned WantedSize);
Description Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content
of the newly allocated block of memory is not initialized, remaining with indeterminate values.
Parameters - WantedSize: Size of the memory block, in bytes.
Returns Nothing.
Requires Nothing.
Example
int *pi; // pointer to integer
int ai[100]; // array of integers
void main() {
pi = (int *)malloc(sizeof ai); // pi will point to a memory block where
the array is allocated
}
Notes The type of this pointer is always void, which can be cast to the desired type of data pointer in order
to be dereferenceable.