Q:
What is dynamic memory fragmentation?
belongs to collection: Embedded C interview questions and answers (2022)
Embedded C interview questions and answers (2022)
- What is the difference between C and embedded C?
- What is the volatile keyword?
- What is the use of volatile keyword?
- What is the difference between the const and volatile qualifiers in C?
- Can a variable be both constant and volatile in C?
- Can we have a volatile pointer?
- The Proper place to use the volatile keyword?
- What is ISR?
- Can we pass any parameter and return a value from the ISR?
- What is interrupt latency?
- How do you measure interrupt latency?
- How to reduce interrupt latency?
- Is it safe to call printf() inside Interrupt Service Routine (ISR)?
- Can we put a breakpoint inside ISR?
- What is the difference between an uninitialized pointer and a null pointer?
- What are the causes of Interrupt Latency?
- Can we use any function inside ISR?
- What is a nested interrupt?
- What is NVIC in ARM Cortex?
- Can we change the interrupt priority level of Cortex-M processor family?
- Why “C” language mostly preferred than assembly language?
- What is the start-up code?
- What are the start-up code steps?
- Infinite loops often arise in embedded systems. How do you code an infinite loop in C?
- How to access the fixed memory location in embedded C?
- Difference between RISC and CISC processor?
- What is the stack overflow?
- What is the cause of the stack overflow?
- What is the difference between the I2c and SPI communication Protocols?
- What is the difference between Asynchronous and Synchronous Communication?
- What is the difference between RS232 and RS485?
- What is the difference between Bit Rate and Baud Rate?
- What is segmentation fault in C?
- What are the common causes of segmentation fault in C?
- What is the difference between Segmentation fault and Bus error?
- Size of the integer depends on what?
- Are integers signed or unsigned?
- What is a difference between unsigned int and signed int in C?
- What is the difference between const and macro?
- How to set, clear, toggle and checking a single bit in C?
- What will be the output of the below C program?
- Write a program swap two numbers without using the third variable?
- What will be the output of the below C program?
- What is meant by structure padding?
- What is the endianness?
- What is big-endian and little-endian?
- Write a C program to check the endianness of the system
- How to Convert little-endian to big-endian vice versa in C?
- What is static memory allocation and dynamic memory allocation?
- What is the memory leak in C?
- What is the output of the below C code?
- What is the output of the below C code?
- What is the difference between malloc and calloc?
- What is the purpose of realloc( )?
- What is the return value of malloc (0)?
- What is dynamic memory fragmentation?
- How is the free work in C?
- What is a Function Pointer?
- How to declare a pointer to a function in C?
- Where can the function pointers be used?
- Write a program to check an integer is a power of 2?
- What is the output of the below code?
- What is the output of the below code?
- Write a program to count set bits in an integer?
- What is void or generic pointers in C?
- What is the advantage of a void pointer in C?
- What are dangling pointers?
- What is the wild pointer?
- What is a NULL pointer?
- What are the post-increment and decrement operators?
- Which one is better: Pre-increment or Post increment?
- How will you protect a pointer by some accidental modification with the pointer address?
- How to use a variable in a source file that is defined in another source file?
- Can static variables be declared in a header file?
- What is the difference between pass by value by reference in c and pass by reference in c?
- What is a reentrant function?
- What is the inline function?
- What is the advantage and disadvantage of the inline function?
- What is virtual memory?
- How can you protect a character pointer by some accidental modification with the pointer address?
- Consider the two statements and find the difference between them?
- Can structures be passed to the functions by value?
- What are the limitations of I2C interface?
- What is the Featured of CAN Protocol?
- What is priority inversion?
- What is priority inheritance?
- Significance of watchdog timer in Embedded Systems?
- What Is Concatenation Operator in Embedded C?
Answer:
The memory management function is guaranteed that if memory is allocated, then it would be suitably aligned to any object which has the fundamental alignment. The fundamental alignment is less than or equal to the largest alignment that’s supported by the implementation without an alignment specification.
One of the major problems with dynamic memory allocation is fragmentation, basically, fragmentation occurred when the user does not use the memory efficiently. There are two types of fragmentation, external fragmentation, and internal fragmentation.
The external fragmentation is due to the small free blocks of memory (small memory hole) that is available on the free list but the program not able to use it. There are different types of free list allocation algorithms that used the free memory block efficiently.
To understand the external fragmentation, consider a scenario where a program has 3 contiguous blocks of memory and the user frees the middle block of memory. In that scenario, you will not get a memory, if the required block of memory is larger than a single block of memory (but smaller or equal to the aggregate of the block of memory).
The internal fragmentation is wasted of memory that is allocated for rounding up the allocated memory and in bookkeeping (infrastructure), the bookkeeping is used to keep the information of the allocated memory.
Whenever we called the malloc function then it reserves some extra bytes (depend on implementation and system) for bookkeeping. This extra byte is reserved for each call of malloc and becomes a cause of the internal fragmentation.
For example,
need an explanation for this answer? contact us directly to get an explanation for this answerSee the below code, the programmer may think that the system will be allocated 8 *100 (800) bytes of memory but due to bookkeeping (if 8 bytes) system will be allocated 8*100 extra bytes. This is an internal fragmentation, where 50% of the heap waste.