Working With The Stack and Heap
Working With The Stack and Heap
Agenda
What is the heap? Determining heap size Potential Problems in Working with the Heap Heap Layout Considerations Working with the Stack Determining stack size Potential Problems in working with the Stack Static stack checkers Stack plug-in in the Embedded Workbench Demonstration
Potential problems
One of the most common problems with dynamic memory allocation occurs when blocks of memory of varying size are frequently allocated and freed When memory is freed, there will be a memory hole This can be a problem if the next allocation is larger than any of the available holes This can lead to difficulties in debugging since the total amount of free space on the heap may be sufficient for a desired allocation but allocations may fail since the free space is not contiguous.
Potential problems
For example, assume the following: The heap lies from 0x20000 - 0x20FFF A small block of memory, 8 bytes, is allocated at the beginning of the stack Immediately following this block of memory is a block of 1024 bytes At some later time, the first block is freed and can be reused. However, the application needs to allocate a block of memory larger than 8 bytes so the free block at the beginning of the heap cannot be used. This shows how the heap can become fragmented, if the application never again requests a block of 8 bytes or less, then the block at the beginning of the heap is wasted. This example also demonstrates how small blocks of memory on the heap are inefficient, the overhead is equal in size to the amount of data available to the application.
Potential problems
Demo
Open up the Embedded Workbench for ARM Choose Example Projects from the Information Center and select a project Check the Project-Options-DebuggerPlugins to make sure it is setup to use the stack plugin Recompile the code to make sure it compiles with no errors or warnings, then download and debug!
Q&A