Using dynamic memory allocation

Dynamic memory allocation is a common practice among C++ developers, and it is widely utilized in the C++ standard library; however, in the context of embedded systems, it often becomes a source of issues that are hard to discover and hard to avoid.

The most notable issue is timing. The worst-case time for memory allocation is not-bound; however, embedded systems, especially those controlling real-world processes or equipment, are often required to respond within a specific amount of time.

Another problem is fragmentation. When memory blocks of different sizes are allocated and deallocated, memory regions appear that are technically free but cannot be allotted because they are too small to fulfill an application request. Memory fragmentation grows over time and can lead to the situation where a memory allocation request fails despite a substantial total amount of free memory.

A simple yet powerful strategy to avoid these types of issue is to allocate all the memory that an application might need in advance at compile time or at startup time. Then the application uses this memory as needed. This memory, once allocated, is never freed until the application terminates.

A disadvantage of this approach is that the application allocates more memory than it really uses at this point in time instead of letting other applications use it. In practice, this is not an issue for embedded applications, since they are running within a controlled environment, where all applications and their memory needs are known in advance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.54.63