Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of malloc in embedded c [closed]

Tags:

c

embedded

In embedded c programming language. Is malloc useful in single task embedded system?

I am working since 0.5 years in embedded system. I never used malloc in 8-bit controller programming.

can anybody suggest me to use malloc in 8-bit controller programming?

like image 512
kallappa Avatar asked Jun 14 '16 12:06

kallappa


People also ask

Can we use malloc in embedded C?

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.

Why dynamic memory allocation is not used in embedded?

Dynamic memory allocation makes it very difficult to debug, especially with some of the limited/primitive debug tools available on embedded system. If you statically allocate stuff then you know where stuff is all the time which means it is much easier to inspect the state of something.

Why malloc is not safe?

In contrast, malloc does not touch the contents of the allocated block of memory, which means it contains garbage values. This could potentially be a security risk because the contents of memory are unpredictable and programming errors may result in a leak of these contents.

What is the use of malloc in C?

Malloc is used for dynamic memory allocation and is useful when you don't know the amount of memory needed during compile time. Allocating memory allows objects to exist beyond the scope of the current block.


2 Answers

In embedded systems the use of dynamic allocation is strongly discouraged. Behavior of the critical systems should be deterministic. Many libraries and OS for embedded firmware avoid using dynamic allocation.

For short explanation why malloc is not good for embedded systems see: malloc sins

Standards for critical systems may prohibit the use of malloc as a bad programming practice.

For example MISRA C1 and MISRA C2 do not allow the use of malloc/calloc. See MISRA standard.

Q&A: dynamic memory allocation - MISRA Bulletin Board

like image 78
sg7 Avatar answered Oct 01 '22 22:10

sg7


No it is not useful. The whole purpose of malloc is to let multiple processes share all available RAM memory of the system dynamically, when they have need for it. This in turn implies that you have a multi-process system and that the amount of available RAM is vast, but also variable or unknown.

In smaller embedded systems that are either "bare metal" (no OS) or use a RTOS, such memory sharing does not make any sense. Unlike a PC, such an embedded system is completely deterministic and therefore you always know the amount of RAM needed for the worst case. You also know exactly how much RAM there is on the chip.

The need of using malloc on such systems typically originates from confused PC programmers who have picked up embedded programming without studying it first.

Details here.

like image 33
Lundin Avatar answered Oct 01 '22 22:10

Lundin