NetBurner 3.5.0
PDF Version |
|
This example demonstrates the usage of malloc and free. It identifies way's to track heap space usage by calling spaceleft() and mallinfo(), which can help prevent an applications from running out of dynamic memory.
It is important to use both spaceleft() and mallinfo() to determine the size of the heap. As this application demonstrates, spaceleft() alone will not always give the total space available to malloc.
Dynamic memory and fragmentation is beyond the scope of this example, but in short, the largest amount of contiguous dynamic memory an application can malloc depends on how the application handles malloc and free. For example:
In this code example the application will allocate 3 chunks of space. The first is 1MB, then a 3MB, then a 512KB chunk. It will then free the data in a different order.
After every malloc and free, the amount of heap space will be displayed by both reporting functions showing heap space used, free, and untouched.