11.07.2015 Views

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dynamic memory on the heapAnother problem is the risk of running out of stack. This will happen when one functioncalls another, which in turn calls a third, etc., and the sum of the stack usage of eachfunction is larger than the size of the stack. The risk is higher if large data objects arestored on the stack, or when recursive functions—functions that call themselves eitherdirectly or indirectly—are used.Dynamic memory on the heapMemory for objects allocated on the heap will live until the objects are explicitlyreleased. This type of memory storage is very useful for applications where the amountof data is not known until runtime.In C, memory is allocated using the standard library function malloc, or one of therelated functions calloc and realloc. The memory is released again using free.In <strong>C++</strong>, there is a special keyword, new, designed to allocate memory and runconstructors. Memory allocated with new must be released using the keyword delete.For <strong>MSP430</strong>X, the compiler supports heaps in both data16 memory and data20 memory.For more information about this, see The heap, page 39.Potential problemsApplications that are using heap-allocated objects must be designed very carefully,because it is easy to end up in a situation where it is not possible to allocate objects onthe heap.The heap can become exhausted if your application uses too much memory. It can alsobecome full if memory that no longer is in use has not been released.For each allocated memory block, a few bytes of data for administrative purposes isrequired. For applications that allocate a large number of small blocks, thisadministrative overhead can be substantial.There is also the matter of fragmentation; this means a heap where small sections of freememory is separated by memory used by allocated objects. It is not possible to allocatea new object if there is no piece of free memory that is large enough for the object, eventhough the sum of the sizes of the free memory exceeds the size of the object.Unfortunately, fragmentation tends to increase as memory is allocated and released. Forthis reason, applications that are designed to run for a long time should try to avoid usingmemory allocated on the heap.22<strong>MSP430</strong> <strong>IAR</strong> C/<strong>C++</strong> <strong>Compiler</strong>Reference Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!