 Hello friends, I am Sanjay Gupta in this video I am going to demonstrate you how to use malloc and free functions of dynamic memory allocation in C programming. Before starting you can note my information you can connect with me by following these details where my youtube channel url is available and you can have name of my android app that is Techimiz. You can search my youtube channel by typing my name on youtube search that is Sanjay Gupta. My channel link will be available there you can open it here various programming related playlists are available you can improve your programming skills by watching these videos you can click on this subscribe button if you want to follow my youtube channel now I am going to demonstrate you how you can use malloc and free functions which are available for dynamic memory allocation in C programming so first time including a header file that is stdlib.h then another header file that is stdlib.h so this stdlib header file contains declarations of malloc and free functions now I am going to define main function inside main I am declaring a pointer p variable n and then variable I now I have to use printf function which will display the message enter size of array so I am going to implement dynamic array in this program that's why I have mentioned this printf statement which will show the message enter size of array now user will be providing a value that will be received through scanf and stored inside n so n will be the value which is responsible for memory allocation now I have to use malloc function which will allocate dynamic memory so I am using this function so this is the function malloc which is responsible for dynamic memory allocation so through malloc function n blocks will be created which will be of a integer type and this integer pointer is used here for typecasting and the base address of this memory location will be stored inside p pointer now with the help of p pointer I can access all the locations of array which is created through malloc function so here you can see I have not used any array that is that is available by the programmer or you can say by me I have just used a pointer and through malloc I have created dynamic array so this is the advantage of dynamic memory allocation here required amount of memory will be allocated as per the user's requirement now malloc function automatically fills all the blocks with garbage value so now I am just printing the blocks which are available through malloc let's see what will happen while I will be executing this program so I am implementing for loop first time using a printf statement which will display the message elements are then for loop which will repeat n times inside this for loop I am writing printf statement which will display the elements on console and for that I am using this statement asterisk p plus i so p is containing base address every time i will be added into base address and it will be dereferenced with the help of this asterisk symbol so all the element which are available in the array will be displayed with the help of addresses now after completion of this loop I can call free function so free function is responsible for deallocating the dynamically allocated memory so it deallocates dynamically allocated memory and at last I am writing return zero so this way through malloc I have allocated memory and through free function I have deallocated the allocated memory through malloc and the output of this program will be garbage values will be displayed n times now I am executing this code you can see it is asking for the size of array so I am entering six it means I have to enter only six elements so right now I have not read any value because I have just printed the element which are available on those six locations so you can see six values are displayed here which are treated as garbage values so malloc is a predefined function which automatically allocates a memory at runtime and fills them with the help with garbage values that you have seen and free function is responsible for deallocating that memory block so that freed space can be used by the another another application that is available with processor so if you want to reuse that memory block then you can use free so use of free is optional in case of dynamic memory location but if you are a good programmer and you want to apply a good programming approach then you have to use free at the end of the program so this way I have demonstrated you the use of malloc and free function you can also receive elements from user in case of malloc so now I am going to show you how you can do this so you have to add this block here you will be printing the message enter elements then this loop will repeat n times and you will have to pass only addresses through p plus i and then elements will be displayed with help of this loop so here you can see I have used loops twice this is for reading and this is for printing elements on console so this way I have implemented this program now which will read n and elements from user so now I am going to execute this code again I am entering size of array as six sorry in place of printf I have to use scanf I forgot because I have copied the statements so this is a drawback of copy paste so you can avoid that again I am executing this code now it is asking for numbers so I am entering six elements you can see the output this time the entered elements are displayed on console properly so I hope you have understood how I have implemented the use of malloc and free function in this program I have demonstrated you this code by two different ways by reading elements from user and by displaying the elements which are provided by malloc so I hope you have understood whatever I have demonstrated you in this video if you want to watch more programming related videos you can follow or subscribe my youtube channel through the url youtube.com slash sanjay gupta underscore tech school you can download my programming app tech immense which is available on google play thank you for watching this video