 Hello friends, welcome back on Sanjay Gupta Tech School. I am Sanjay Gupta and you are watching C language video series. So in this video, the topic we are going to discuss is dynamic memory allocation. So first of all we will see what is the difference between static and dynamic memory allocation. Then in dynamic memory allocation, we get four predefined functions. So in the previous video, I told you that we can use array pointer, string pointer and structure pointer. And in the previous video, we have given the basis of pointer. So after reading the pointer, you must have understood that we can store any variable's address in the pointer and process it through the pointer. But you must have thought that why are we using pointer basically. When the variable is there, you need to make the pointer separate. So you will understand that in this video. So first we will see the differences and after that we will discuss how dynamic memory allocation is implemented. So in this, you will see that I have given four differences. So if we talk about static memory allocation, you have seen all the videos in this video series. I am talking about this video series. In this video series, all the videos you have seen so far, we have made all the variables, arrays, strings, structure, anything that is working to store data. All those types were static. So in the case of static, the size of memory is provided by the programmer. The size of the programmer will be set according to the user. But if you want the user to tell you that I have so many memory allocations and then I store the value. So that is known as dynamic memory allocation. So have we done this so far? No. In this video series, we have never asked the user how much memory you want to allocate and then we have asked for values from it. We directly tell it to enter these numbers like 2 number, 5 number, 10 number, 20 number. So we input it this way. Then the second difference is in the case of static, at the run time, you cannot change the size of memory. We have made an array, integer type, size of 10. So can you make its size 20 on the run time? It is not possible. You will have to change it in the code itself and then you can change it. But in the case of dynamic, we ask the user how much memory you want to allocate. So at the run time, you decide and at the run time, you store the value by allocating the memory. Even if you want to change the size of the allocated memory, then reallocate is a method. I have told you here, reallocate. So through this, we can reallocate the allocated memory. Then in the case of static, there will be wastage of memory. Like if we take an example of string. In string, we have mentioned the size of 50. And in your name, there are 6 or 7 characters. So the rest of the memory will be wasted. So what we do in that case? In that case, we will ask the user how much memory you want to allocate. So if you have 10 characters in your name, then you allocate 10 blocks and use the same memory. Then in the case of dynamic, memory will not be wasted. And lastly, there is no point in the static. Whereas in the case of dynamic, there is a requirement of pointers. So this is what I have told you. Now after these differences, we will discuss how to implement dynamic memory allocation. So first I will tell you how to implement malloc. So first we will focus on malloc. With which you will understand dynamic memory allocation. After that, we will use teloc-free and vloc. This is written from the beginning as dynamic memory allocation. The header file includes stdio.h and main. So now if we want to discuss, we have to read the n numbers from the user. Means once I have run the program, then 10 numbers should come. If I have run the program, then 15. If I have run the third time, then 5. And the same blocks should be allocated. So normally we make an array like this. So if we make an array like this, then the size is fixed to 10. So you have to run the loop 10 times. Means every time 10 numbers will come. Whenever you want to change the quantity of the numbers, then you have to change it here. Then you have to change the execution cycle of all the loops. If we make an array like this, then we directly make a pointer. So now you will see that I am declaring a pointer. There will be no array declare anywhere. So now you will understand what is the need for a pointer. Why should we use a pointer so much? So I have declared a pointer. After declaring a pointer, then there will be an i variable which will run the loop. There will be an n variable which will read the size. And there will be an s variable which will store the sum. So these three additional variables are i, loop, counter, n which will read the size and s which will have total sum. I have not made any variable to store the elements. Simply I have declared a pointer. Now we will write a printf. We will write it in printf, enter size of memory. We will scan it and it will come in n variable. So now the user has told me that I have to allocate so many blocks. So now we will come to maloc function. Maloc function will help you to dynamically allocate memory. So for that we will write p equals to integer pointer. Then maloc size of int multiplied by n. Now let us understand this. Maloc is a predefined function which works to allocate memory. Size of int is telling you how many memory is required for an integer variable. And how many memory is required for an integer variable. And how many memory is required for an integer variable. So let us suppose n is value of 5. So it means we have to store 5 values. So how many integer types of blocks we have to make. So we have multiplied n5 by size of n. So the amount of memory allocated will be typecast in integer pointer. And the base address of it will be p point. So this way dynamic memory is allocated. So now if we make a diagram of it. So I will try to explain the diagram through the paint. So let us say this is our p pointer. Initially p pointer was blank. Then we made another variable n. And we fixed the value of n as 5. Now we are using maloc. So here n5 and size of int is 32 bit. In the case of compiling, int is 4 bytes. Means 20 bytes will be allocated. So how will it be allocated? It will be a complete memory block. Of 20 bytes. And internally how many parts will be divided into 5 parts. And the base address of it. The beginning address of it will be p pointer. So if we have base address available. Then we can make it plus plus. Like I told in the last video. When we discussed area and pointer. If we have any area base address available. Then we will make the pointer plus plus. So it will jump according to that type. So the point of peak type is of integer type. Because we know that we store the value of integer type. So when I will make the peak plus plus. Then how many bytes will it jump? 4 bytes. Because it is a 32 bit compiler. If it is a 16 bit compiler, it will jump 2 bytes. Similarly when I will jump 4 bytes. Then I will come here. Then I will jump 4 bytes. Then on this location. Then on this location. So in this way I can process each and every location. If I change the size. I have run the program for the first time. I have run 5 inputs. I have run 5 blocks. Next time I have run the program again. And I have entered the size of n. So automatically I will make 10 blocks. And please manage it. So this is the statement. This is what we have used. For dynamic memory allocation. Now let's run a loop. How many times will the loop run? Because we don't know the exact size. But we know the size of n. So we have run the loop for n time. Then we have written s equals to s plus asterisk p. Now we have written asterisk p. So now you can do 2 things. Either you can do p plus plus. P will go ahead. But the problem will be that the original base location of p can be changed to go to the next location. So instead of that you can do this. You write p plus i. So what will happen with this? P has base address. In that I added. I initially 0. So this base address will be p and it will be de-referenced. When we will do i plus plus. So p plus 1. It means add 1 in the address in p. So add 1 means to reach the next location. To jump as many bytes as the pointer type. So pointer type int. So it will jump 4 bytes and reach the next location. And it will be de-referenced. Then next time it will be i plus plus. It will be i 2. So base address. P has the same base address. P is not changing. So p plus i. So p means base address plus 2. So how many times it will jump? 2 times. 1 times 4 bytes and then 4 times 2. So it will reach the second location. Means second index. So it is already at 0. So it is already at 1. So it is already at 2. So it is already at 1. So it is already at 1. So it is already at 1. So it is already at 1. So it is already at 1. So it is already at 1. So it is already at 2. So 1 is a function of p of sup-l Essentially 0x and 2 is same. So it is already at 1. So it is already at 2. So it is already at 1. And that it works. So it is already at 1. So this is already at history data. Everything all the other functions are same. 0x already at values. now let me entered size 5 and now i'm entering 5 values let me enter sum 5 now i won't change anything in code, i'll run program again and this time i'll do memory size 10 enter 10 values, automatically 10 blocks are allocated now i'll enter 10 time value and sum 10 watch there was a difference in static and dynamic यह आप कप पुओओओओ ना है, पाई उसाईए अग़ान आ वी लोकेट करनेव ओग़े आप युमुट कराशाःःियाता अबगी दग जो स्ब पने आश्ड़िैक एर्वें एप यह नहीं ते, अप कि भि जो फो रफ ते, येप लिए रव्याजी ग़िए आप वो पने उपके सब ज़ासे चिए कुओदि थे. आ táपा रई आखigation Österreich पनुटनंबी उसक्तझा Juliet configuration वो मैंवरी अलोकेशन करता है, दिस्विबिटेद, वतलाब जहाँपे उसको मैंवरी मिली, जे से 20 बाइट्स अलोकेट करनाखा, साईस पाइप है, इंट को 4 बाइट मिला, तो तोटल 20 बाइटस वो अलागलाग लोकेशन पर भी मैनेच कर लिएता है, बत कैलोक को कन्तिनेस मैंट्स मैंमरी अलोकेट लोकेट करता वो औगर मैंच कर लिएता है, वह क्याँभ लोक जगेगेगेगेगेगेगेगेगेगे खऱ्ुएस पुहलेए एकचि्विबिटेट होगा, अर उसके जबाद ही मैं ठवोच्टेद हो परणग करागता हो मृत्टिप जो उसके दिनैमिक्ली प्य़िएलोकेट हूँ ऊस्को समख प्य़िएलोकेट रहागता हो स्यो denamity memory is allocated watch. तिओ नागता है आफ भागते लिए निए बलडोग ये लेगता है तो कलोग के ले में एसको सेव किया, रन किया, मैंने फाइप अंटर किया तो आप देख सकते हो दिनामेक लिए लोकेटेद मैमरी वेलूँस क्या प्रिंत हो रहा है सारे ब्लोक्स जीरो है, पाजो ब्लोक्स में अटमेतिकली क्या इरीश्लाइस हो गया, जीरो फिर आप फिर आम कुट से वेलूँस डाल रहें, तो वो वेलूँस इंझफुट करागगे उनका सम प्रिंत रहा है टिके अब आप यहां पर चलष़न आप लगा देख आप शोगा लिए किए निए लाईई में वेलूँस प्रिंत हो अप दिखतें की आखर मलोग कोततो क्या अता, मलोग के खीस में बिगवोग अतलाग क्या वेल्यूज धीनीशिल़ाईस होती, अप देक सथ ए वेल्ग या कंगर रभेज लीज लाईज , अप जाते हो ण्दिनगे यह रब उस्ब अज्डवी एर उक कर तो आप छाते है की अद तो इस तरीके से आपको कलोग और मैलोग समज में आगा होगा अब हम बखत करते हैं वी अलोग की और इसके बाद फिरे एक एक एकजामपल और देखेंगे जिस में हम श्रिंग को भी इसके तो इंपलमेंकर के देखेंगे अब हम पर है आपको बष्रिंग के समब आपको है वी नलको अगर मैंच़ा करना रहेगा अब आपको यहाप साओ़ी अंपुत कराईा फिर मेमरी अलोकेट की आप फिर अलोकेट मेमरी की वगॉष को दिस्पले कराईा उप्छिए, वेलूऑ रीउज रीउद कराए विद्च्यजे । । थोब हम ने सम खलख्लेट क्या छीगे, सम पर्रिंट करादीअ, अब हम चाहतार है की हम जो पोंटरस को फिरषे ने वेलूच मैंग्ये लोकेट कराइ। after that you have to pass the pointer and after that you have to pass the size so the pointer is p and after that the size is available in m so the new memory will be allocated and after that you can use it as it is I am copying it and pasting it here so now let us see our program from the beginning firstly we read the size, we allocate the memory through malloc then we printed the initial values then we input the user values and printed the sum then we read the new size, we allocate it through realoc means the new pointer will point the new memory location according to the end then this loop will go to the end times means the new size will read the values then the sum will be there and the sum will be printed so let us save it and run it so first I entered the size 5 after that I entered the new size 5 now it is asking to enter the new size so I entered 10 now it is asking to enter 10 values so the pointer is automatically relocated to 10 locations now this is the problem because it displayed 15 here because it displayed 15 because the sum variable I did not initialize it now it is initializing the new size 10 so after printing the sum here we will have to initialize the sum again so the results will be displayed properly so now if we execute it again so first 5 then 5 values 5 sum is here now I have added 7 so 7 values so it is executed properly so normally we do not prefer re-alloc we run it through malloc-calloc and now I will tell you how to process the characters so we will erase this now we will have to read the size for the character here the character pointer will come here the care will come now the integer pointer will be converted to the character pointer and size sum so we have made the size input from the user we have allocated the memory we are not printing the initial values now we have to make the string input from the user so we will write enter string and to read the string you do not need a loop you just have to pass the p pointer so in the p pointer all the values will be input and you know the size but now even after the size input the user can get less characters input so what you have to do is simply check the condition so you have to put asterisk p plus i not equals to null so asterisk p plus i will add the base address in the i and if there is no null on the index then it will go and what we have to do here is to put the sum variable so it will exactly count how many characters you have input so you have given the size to 10 but you don't have 6 characters in your name so you have put 6 inputs but when we are using dynamic memory location then normally this will not happen so you can apply some other logic like you can read the string and print or you can apply some other logic or you can copy one string or you can perform any operation so let's run it once this is not a double course so let's show it look this is run memory size 10 now it didn't read the string so the same problem came here that as soon as I entered it took the string to enter so what you have to do is so this is the problem with C which has been resulting in C++ Java sorry, get care so memory size 10 now it is asking to enter string so I have entered so look at length 6 so in this way we have processed the allocated area so you will see that we have not declared the string the character area we have not declared it anywhere we have performed it directly so I hope now you will understand how dynamic memory allocation is implemented what is its use so this is also clear why we have stood in the pointer because if you don't know the pointer then you don't understand this video so if you have any doubt you can discuss it the details are available and I hope whatever I tried to explain you will understand how to implement dynamic memory allocation through dynamic memory allocation so that you can understand because in exams many times the programs of array and string are dynamically implemented so you should know and the playlist link of this video is in the description of this video and if you are available on my channel then you will see many more playlists so thank you for watching this video