In C and C++, it can be very convenient to allocate and de-allocate blocks of memory as and when needed. This is certainly standard practice in both languages and almost unavoidable in C++. However, the handling of such dynamic memory can be problematic and inefficient. For desktop applications, where memory is freely available,these difficulties can be ignored. For real-time embedded systems, ignoring the issues is not an option. Dynamic memory allocation tends to be non-deterministic; the time taken to allocate memory may not be predictable and the memory pool may become fragmented, resulting in unexpected allocation failures. In this paper the problems will be outlined in detail. Facilities in the Nucleus RTOS for handling dynamic memory are outlined and an approach to deterministic dynamic memory allocation detailed.
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Dynamic Memory Allocation & Fragmentation in C & C++
1. mentor.com/embedded
Android is a trademark of Google Inc. Use of this trademark is subject to Google Permissions.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
Colin Walls
colin_walls@mentor.com
Dynamic Memory
Allocation &
Fragmentation in C & C++
2. Agenda
Introduction to memory usage
Dynamic memory in C
Dynamic memory in C++
Issues and problems
Memory with an RTOS
Real time memory solutions
Conclusions
3. Agenda
Introduction to memory usage
Dynamic memory in C
Dynamic memory in C++
Issues and problems
Memory with an RTOS
Real time memory solutions
Conclusions
7. C/C++ Memory Spaces
With an operating system,
there are typically multiple
stacks
RAMaddress
Static storage
Dynamic storage
Stack
Heap
Variables
Stack
Stack
Stack
8. Agenda
Introduction to memory usage
Dynamic memory in C
Dynamic memory in C++
Issues and problems
Memory with an RTOS
Real time memory solutions
Conclusions
9. mentor.com/embedded
Android is a trademark of Google Inc. Use of this trademark is subject to Google Permissions.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
To read the rest of the whitepaper,
see the full version on Mentor
Embedded.