In many embedded systems applications, developers are often caught on the horns of a dilemma. On the one hand, they are constrained to use the limited memory space available as efficiently as possible. But at the same time, they are required to get as much performance out of the hardware resources (including memory) as is possible.
So it's no surprise that embedded systems developers spend much of their time trying to manage the available memory resources so as to achieve the best combination of performance and minimum memory use. To do this, extensive use is made of a variety of static and dynamic memory allocation techniques.
In static memory allocation, it's necessary to predict–and limit–in advance what the maximum number of objects will be for a given pool, or seen from another perspective, the amount of memory.
This often leads to a large amount of memory being “wasted,” and/or constraints on the number of objects that would limit functionality.
Another problem with static memory allocation has to do with security: allocating more memory on the stack than is available can result in a crash due to stack overflow.
A third problem is that the memory stored on the stack is automatically deallocated when the function that created it returns, and thus the function must copy the data if they should be available to other parts of the program after it returns.
But despite these drawbacks, static memory allocation is still an important part of the embedded systems developer's repertoire because it's much faster than the allocation on the heap, as the data is added and removed in a last-in-first-out manner. Also, the memory on the stack is automatically reclaimed when the function exits, which can be convenient for the programmer.
But on balance, the few advantages of static allocations are far outweighed by its disadvantages in favor of the dynamic approach. Allocating memory dynamically means allocating it from a large pool of unused memory called heap (also called free store).
The word dynamic, related to the memory allocation, refers to two things:
• Allocating memory during the runtime of the program.
• The object allocated has a dynamic lifetime as it is not deallocated when a function returns.
But while dynamic memory allocation is more versatile and secure than static memory allocation, it, too, has some performance-related issues. One has to do with memory leaks. As the memory is not freed automatically, the programmer has to take care of this task and remember to free the memory. It's also necessary to take the time to find a block of unused memory of sufficient size to fulfill the request.
To access the digital edition, where you can download a PDF, click here. You may need to fill out a short form first. The feature articles are available in the digital edition but will be posted online in full at the end of the month.
To access the rest of this article, click here. Currently the article is only available via the free digital edition of Embedded Systems Design magazine but will be posted in full online at the end of the month.
Note: If you haven't registered for the free digital edition, we've improved the interface for doing so: click the link above, fill out a short form, and view the digital edition (where you can download a PDF if you wish). You only have to sign up once for unlimited access to the digital edition. Questions or comments? Send them to Susan Rambo at .