Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is realloc() safe in embedded system?

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?

like image 930
Jeegar Patel Avatar asked Aug 25 '11 09:08

Jeegar Patel


People also ask

Is realloc safe?

It's perfectly safe to use realloc . It is the way to reallocate memory in a C program. However you should always check the return value for an error condition.

Does realloc destroy data?

Will I lose my data? No, the data will be copied for you into the new block that the returned p points at, before the old block is freed. This all happens before realloc returns, so the new p points to your data still.

Does realloc save data?

realloc should preserve the previous data if the pointer supplied in the call is a valid pointer to the memory region previously allocated and the new size is larger than the previously allocated size. If the pointer supplied in the realloc call is null, then it acts just like a malloc.

Does realloc copy data?

The value of realloc is the new address of the block. If the block needs to be moved, realloc copies the old contents. If you pass a null pointer for ptr , realloc behaves just like ' malloc ( newsize ) '.

What is the purpose of realloc ()?

realloc deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object is identical to that of the old object prior to deallocation, up to the lesser of the new and old sizes.

Why malloc is not used in embedded systems?

There are a number of reasons why malloc() is not generally recommended for embedded applications: The function is commonly not re-entrant (thread friendly), so using it with a real-time operating system may be challenging.


1 Answers

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:

  • MISRA-C:2004 Rule 20.4 "Dynamic heap memory allocation shall not be used."
  • IEC 61508 Functional safety, 61508-3 Annex B (normative) Table B1, >SIL1: "No dynamic objects", "No dynamic variables".
like image 173
Lundin Avatar answered Sep 20 '22 15:09

Lundin