10 questions about dynamic memory allocation, your interviewer might ask.

interview question on dynamic memory allocation in c

In the beginning of the career, many developers fear to use the dynamic memory allocation. In this article, I will explain 10 interview question on dynamic memory allocation in c.

What is the difference between malloc and calloc?

The malloc and calloc are memory management functions. They are used to allocate memory dynamically. Basically, there is no actual difference between calloc and malloc except that the memory that is allocated by calloc is initialized with 0. In C language,calloc function initialize the all allocated space bits with zero but malloc does not initialize the allocated memory. These both function also has a difference regarding their number of arguments, malloc take one argument but calloc takes two.

Is it better to use malloc () or calloc ()?

The calloc function initialize the allocated memory with 0 but malloc don’t. So the memory which is allocated by the malloc have the garbage data. In another word you can say that calloc is equal to the combination of malloc and memeset.

See the below expression,

ptr = calloc(nmember, size);  //is essentially equivalent to

ptr = malloc(nmember * size);
memset(ptr, 0, (nmember * size));

Note: If you don’t want to initialize the allocated memory with zero, It would be better to use malloc over calloc.

How can you determine the size of an allocated portion of memory?

In C language, we can calculate the size of the static array using the sizeof operator but there is no operator to calculate the size of the dynamically allocated memory.

Mainly there are two ways to get the size of allocated memory in every section of the code.

  • Create a global variable to store the size of the allocated memory.
  • Carry the length of allocated memory.

For example,

Suppose you need to create an integer array whose size is n. So to carry the array length of the array, you need to allocate the memory for n+1.

int *piArray = malloc ( sizeof(int) * (n+1) );

If memory is allocated successfully, assign n (size of the array) its 0 places.

piArray[0] = n;
* piArray = n;

Now it’s time to create a copy of original pointer but to left one location from the beginning.

int * pTmpArray = piArray +1;

Note: if you are new, see this article arithmetic operation on the pointer.

Now, whenever in a program you ever required the size of the array then you can get from copy pointer.

ArraySize = pTmpArray[-1];

After using the allocated memory don’t forget to deallocate the allocated memory.

free (piArray);

What is the purpose of realloc( )?

void *realloc(void *ptr, size_t size);

The realloc function is used to resize the allocated block of the memory. It takes two arguments first one is a pointer to previously allocated memory and the second one is the newly requested size. The calloc function first deallocates the old object and allocates again with newly specified size. If the new size is lesser to the old size, the contents of the newly allocated memory will be same as prior but if any bytes in the newly created object goes beyond the old size, the values of the object will be indeterminate.


pcBuffer = aticle
pcBuffer = aticleworld

Note: It should only be used for dynamically allocated memory but if ptr is the null pointer, realloc behaves like the malloc function.

For learning more, you can signup for the free trial of this popular c video course by Kenny Kerr.

What is static memory allocation and dynamic memory allocation?

According to C standard, there are four storage duration, static, thread (C11), automatic, and allocated. The storage duration determines the lifetime of the object.

The static memory allocation:

Static Allocation means, an object has external or internal linkage or declared with static storage-class. It’s initialized only once, prior to program startup and its lifetime is throughout the execution of the program. A global and static variable is an example of static memory allocation.

The dynamic memory allocation:

In C language, there are a lot of library functions (malloc, calloc, or realloc,..) which are used to allocate memory dynamically. One of the problems with dynamically allocated memory is that it is not destroyed by the compiler itself that means it is the responsibility of the user to deallocate the allocated memory.

When we allocate the memory using the memory management function, they return a pointer to the allocated memory block and the returned pointer is pointing to the beginning address of the memory block. If there is no space available, these functions return a null pointer.

What is the return value of malloc (0)?

If the size of the requested space is zero, the behavior will be implementation-defined. The return value of the malloc could be a null pointer or it shows the behavior like that size is some nonzero value.It is suggested by the standard to not use the pointer to access an object that is returned by the malloc while size is zero.

What is the memory leak in C?

A memory leak is a common and dangerous problem. It is a type of resource leak. In C language, a memory leak occurs when you allocate a block of memory using the memory management function and forget to release it.

Note: once you allocate a memory than allocated memory does not allocate to another program or process until it gets free.

For more detail see this article, Problem with dynamic memory allocation

What is dynamic memory fragmentation?

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 program not able to use it. There are different types of free list allocation algorithms that used the free memory block efficiently.

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 is larger than a single block of memory (but smaller or equal to the aggregate of the block of memory)

External Fragmentation

The internal fragmentation is the 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 byte (depend on implementation and system) for bookkeeping. This extra byte is reserved for each call of malloc and become a cause of the internal fragmentation.

For example,

See the below code, the programmer may think that 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.

Here is offer for you, use the below promo code and get 15% off any Enterprise IT & Linux System Administration Training courses (End 28 Feb -2018)
Promo code: PRESIDENTS15

 How is the free work?

When we call the memory management functions (malloc, calloc or realloc) then these functions keep extra bytes for bookkeeping. Whenever we call the free function and pass the pointer that is pointing to allocated memory, the free function gets the bookkeeping information and release the allocated memory. Anyhow if you or your program change the value of the pointer that is pointing to the allocated address, the calling of free function give the undefined result.


OutPut: Undefine Result

What is dangling pointer?

Generally, dangling pointers arise when the referencing object is deleted or deallocated, without changing the value of the pointers. It creates the problem because the pointer is still pointing the memory that is not available. When the user tries to dereference the daggling pointers than it shows the undefined behavior and can be the cause of the segmentation fault.

In simple word, we can say that dangling pointer is a pointer that not pointing a valid object of the appropriate type and it can be the cause of the undefined behavior.

Let’s see the below image for the better understanding.

In the image Pointer1, Pointer2 are pointing a valid memory object but Pointer3 is pointing a memory object that has been already deallocated. So Pointer3 become a dangling pointer when you will try to access the Pointer3 than you will get the undefined result or segmentation fault.

You can see,

15 Common mistakes with memory allocation.

100 c interview questions, your interviewer might ask.



  1. Nice Article.
    Is there any way to print allocated dynamic memory block header ? I know that is maintained by memory management functions.

Leave a Reply