Qualcomm Interview Question
Software Engineer / DevelopersBenefits of not using malloc in embedded:
Fixed size allocation allow memory allocation with
constant execution time (no fragmentation). The memory
release for thousands of objects in a pool is just one
operation, not one by one if malloc is used to allocate
memory for each object.
Fixed size allocation can be grouped in hierarchical tree
structures, which is suitable for special programming
structures like loops and recursions.
Fixed-size block memory pools do not need to store
allocation metadata for each allocation, describing
characteristics like the size of the allocated block.
Particularly for small allocations, this provides a
substantial space savings.
Malloc uses variable sized blocks in allocating memory. Using variable sized blocks has the disadvantage of leaving holes in the memory [fragmentation], which may lead to certain memory requests not being satisfied even though there is enough memory in the system. Memory defragmentation is a very costly exercise and not even general purpose machines do it automatically, let alone embedded systems. So, most embedded systems use fixed size blocks for memory allocation. This has the advantage that all the holes that are created in the system are of the same size which is that of the fixed size block. Hence there is no need for defragmentation.
- Bandicoot April 02, 2010