While developing a piece of software for embedded system I used realloc()
function many times. Now I've been said that I "should not use realloc()
in embedded" without any explanation.
Is realloc()
dangerous for embedded system and why?
While developing a piece of software for embedded system I used realloc()
function many times. Now I've been said that I "should not use realloc()
in embedded" without any explanation.
Is realloc()
dangerous for embedded system and why?
Yes, all dynamic memory allocation is regarded as dangerous, and it is banned from most "high integrity" embedded systems, such as industrial/automotive/aerospace/med-tech etc etc. The answer to your question depends on what sort of embedded system you are doing.
The reasons it's banned from high integrity embedded systems is not only the potential memory leaks, but also a lot of dangerous undefined/unspecified/impl.defined behavior asociated with those functions.
EDIT: I also forgot to mention heap fragmentation, which is another danger. In addition, MISRA-C also mentions "data inconsistency, memory exhaustion, non-deterministic behaviour" as reasons why it shouldn't be used. The former two seem rather subjective, but non-deterministic behaviour is definitely something that isn't allowed in these kind of systems.
References:
It depends on the particular embedded system. Dynamic memory management on an small embedded system is tricky to begin with, but realloc
is no more complicated than a free
and malloc
(of course, that's not what it does). On some embedded systems you'd never dream of calling malloc
in the first place. On other embedded systems, you almost pretend it's a desktop.
If your embedded system has a poor allocator or not much RAM, then realloc
might cause fragmentation problems. Which is why you avoid malloc
too, cause it causes the same problems.
The other reason is that some embedded systems must be high reliability, and malloc
/ realloc
can return NULL
. In these situations, all memory is allocated statically.
In many embedded systems, a custom memory manager can provide better semantics than are available with malloc/realloc/free. Some applications, for example, can get by with a simple mark-and-release allocator. Keep a pointer to the start of not-yet-allocated memory, allocate things by moving the pointer upward, and jettison them by moving the pointer below them. That won't work if it's necessary to jettison some things while keeping other things that were allocated after them, but in situations where that isn't necessary the mark-and-release allocator is cheaper than any other allocation method. In some cases where the mark-and-release allocator isn't quite good enough, it may be helpful to allocate some things from the start of the heap and other things from the end of the heap; one may free up the things allocated from one end without affecting those allocated from the other.
Another approach that can sometimes be useful in non-multitasking or cooperative-multitasking systems is to use memory handles rather than direct pointers. In a typical handle-based system, there's a table of all allocated objects, built at the top of memory working downward, and objects themselves are allocated from the bottom up. Each allocated object in memory holds either a reference to the table slot that references it (if live) or else an indication of its size (if dead). The table entry for each object will hold the object's size as well as a pointer to the object in memory. Objects may be allocated by simply finding a free table slot (easy, since table slots are all fixed size), storing the address of the object's table slot at the start of free memory, storing the object itself just beyond that, and updating the start of free memory to point just past the object. Objects may be freed by replacing the back-reference with a length indication, and freeing the object in the table. If an allocation would fail, relocate all live objects starting at the top of memory, overwriting any dead objects, and updating the object table to point to their new addresses.
The performance of this approach is non-deterministic, but fragmentation is not a problem. Further, it may be possible in some cooperative multitasking systems to perform garbage collection "in the background"; provided that the garbage collector can complete a pass in the time it takes to chug through the slack space, long waits can be avoided. Further, some fairly simple "generational" logic may be used to improve average-case performance at the expense of worst-case performance.
realloc
can fail, just like malloc
can. This is one reason why you probably should not use either in an embedded system.
realloc
is worse than malloc
in that you will need to have the old and new pointers valid during the realloc
. In other words, you will need 2X the memory space of the original malloc
, plus any additional amount (assuming realloc
is increasing the buffer size).
Using realloc
is going to be very dangerous, because it may return a new pointer to your memory location. This means:
realloc
.realloc
must be atomic. If you are disabling interrupts to achieve this, the realloc
time might be long enough to cause a hardware reset by the watchdog.Update: I just wanted to make it clear. I'm not saying that realloc
is worse than implementing realloc
using a malloc
/free
. That would be just as bad. If you can do a single malloc
and free
, without resizing, it's slightly better, yet still dangerous.
malloc()
/free()
? –
L The issues with realloc() in embedded systems are no different than in any other system, but the consequences may be more severe in systems where memory is more constrained, and the sonsequences of failure less acceptable.
One problem not mentioned so far is that realloc() (and any other dynamic memory operation for that matter) is non-deterministic; that is it's execution time is variable and unpredictable. Many embedded systems are also real-time systems, and in such systems, non-deterministic behaviour is unacceptable.
Another issue is that of thread-safety. Check your library's documantation to see if your library is thread-safe for dynamic memory allocation. Generally if it is, you will need to implement mutex stubs to integrate it with your particular thread library or RTOS.
Not all emebdded systems are alike; if your embedded system is not real-time (or the process/task/thread in question is not real-time, and is independent of the real-time elements), and you have large amounts of memory unused, or virtual memory capabilities, then the use of realloc() may be acceptable, if perhaps ill-advised in most cases.
Rather than accept "conventional wisdom" and bar dynamic memory regardless, you should understand your system requirements, and the behaviour of dynamic memory functions and make an appropriate decision. That said, if you are building code for reuability and portability to as wide a range of platforms and applications as possible, then reallocation is probably a really bad idea. Don't hide it in a library for example.
Note too that the same problem exists with C++ STL container classes that dynamically reallocate and copy data when the container capacity is increased.
Well, it's better to avoid using realloc if it's possible, since this operation is costly especially being put into the loop: for example, if some allocated memory needs to be extended and there no gap between after current block and the next allocated block - this operation is almost equals: malloc + memcopy + free.
realloc()
any worse than malloc()
/free()
? –
L realloc
should never be worse than malloc
/ memcpy
/ free
, because it could always just do that, and it might obviate the memcpy
. –
Bronchitis © 2022 - 2024 — McMap. All rights reserved.
realloc()
and notmalloc()
oralloca()
? I think I remember seeingrealloc()
criticised in a book because of its highly overloaded behaviour, once, but I don't really go along with that, and I'd be disappointed if anybody saw fit to prohibit it on those grounds. – Kendall