Professional Documents
Culture Documents
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
At the beginning of the career, many developers fear to use dynamic memory
allocation. In this article, I will explain 10 interview questions on dynamic memory
allocation C.
If you are looking for interview questions on dynamic memory allocation C or tricky
interview questions on memory allocation in c, then you are at the right place.
Here I have created the top 10 interview questions on dynamic memory allocation
C with answers which might ask by your interviewer in C interviews. So, I hope
these interview questions on dynamic memory allocation in C will be helpful for
you. So finally Best of Luck.
If you are new and want to learn dynamic memory allocation in C from basic, then
you can check the below articles on dynamic memory allocation.
Besides that, if you have any other doubts regarding the memory management
function, feel free to send an email to me or you can comment in the comment
box. We will try to solve your problem as soon as possible.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
A 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 takes one argument but calloc takes
two.
Note: If you don’t want to initialize the allocated memory with zero, It would be
better to use malloc over calloc.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Let see an example code, where I am explaining how you can carry the length of
the array. So 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.
piArray[0] = n;
or
* piArray = n;
Now it’s time to create a copy of the original pointer but to left one location from
the beginning.
Note: if you are new, see this article arithmetic operation on the pointer.
Now, whenever in a program you require the size of the allocated dynamic, you
get from the temporary pointer(copy pointer).
After using the allocated memory don’t forget to deallocate the allocated
memory.
free (piArray);
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *pcBuffer = NULL;
/* Initial memory allocation */
pcBuffer = malloc(8);
strcpy(pcBuffer, "aticle");
printf("pcBuffer = %s\n", pcBuffer);
/* Reallocating memory */
pcBuffer = realloc(pcBuffer, 15);
strcat(pcBuffer, "world");
printf("pcBuffer = %s\n", pcBuffer);
free(pcBuffer);
return 0;
}
Output:
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.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
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.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
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.
Let see an example C program, where I am allocating memory using the malloc
with size 0
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int *piBuffer = NULL;
//allocating memory using
//the malloc with size 0.
piBuffer = malloc(0);
//make sure piBuffer is valid or not
if (piBuffer == NULL)
{
// allocation failed, exit from the program
fprintf(stderr, "Out of memory!\n");
exit(1);
}
*piBuffer = 10;
printf("%d\n",*piBuffer);
free(piBuffer);
return 0;
}
Output: Implementation-dependent.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
#include<stdio.h>
#include<stdlib.h>
int main ()
{
char * pBuffer = malloc(sizeof(char) * 20);
/* Do some work */
return 0; /*Not freeing the allocated memory*/
}
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.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
The external fragmentation is due to the small free blocks of memory (small
memory hole) that are 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.
Consider a scenario where the 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 is larger than a single block of memory. See the
below Image,
The internal fragmentation is the wastage of memory that is allocated for rounding
up the allocated memory and in bookkeeping (infrastructure). The bookkeeping
memory is used to keep the information of allocated memory.
Whenever we called 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.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
In the below code, a programmer may think that the system will allocate 8 *100
(800) bytes of memory but due to bookkeeping, (if bookkeeping bytes is 8) system
will allocate 8*100 extra bytes. You can see how internal fragmentation is waisting
the heap memory. This is only for understanding actual behavior is the
implementation-dependent.
char *acBuffer[100];
int main()
{
int iLoop = 0;
while(iLoop < 100)
{
acBuffer[iLoop ] = malloc(8);
++iLoop
}
}
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Anyhow if you or your program change the value of the pointer that is pointing to
the allocated address, the calling of the free function gives the undefined result.
Let us see a program to understand the above concept. The behavior of the below
program is not defined.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *pcBuffer = NULL;
//Allocate the memory
pcBuffer = malloc(sizeof(char) * 16);
//make sure piBuffer is valid or not
if (pcBuffer == NULL)
{
// allocation failed, exit from the program
fprintf(stderr, "Out of memory!\n");
exit(1);
}
//Increment the pointer
pcBuffer++;
//Call free function to release the allocated memory
free(pcBuffer);
return 0;
}
OutPut: Undefined Result
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
In simple words, 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.
In the image Pointer1 and Pointer2 is pointing to a valid object but Pointer3 is
pointing an object that has been already deallocated. So Pointer3 becomes a
dangling pointer when you will try to access the Pointer3 than you will get the
undefined result or segmentation fault.
#include<stdio.h>
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
#include<stdlib.h>
int main()
{
int *piData = NULL;
//creating integer of size 10.
piData = malloc(sizeof(int)* 10);
//make sure piBuffer is valid or not
if (piData == NULL)
{
// allocation failed, exit from the program
fprintf(stderr, "Out of memory!\n");
exit(1);
}
free(piData); //free the allocated memory
*piData = 10; //piData is dangling pointer
printf("%d\n",*piData);
return 0;
}
Output: undefined.
I have tried to collect mostly basic interview questions related to dynamic memory
allocation and deallocation. I hope these interview questions on dynamic memory
allocation will helpful. But it is my request if you have any points to improve this
article, please let me know. If you want to add something in this Artice, then
please write comment in comment box or send me an email.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials