Hello everyone, my name is (name)Atharva and we are continuing with this C programming course. (6 seconds pause)
In the last video we had discussed how we can make variable argument functions and we had discussed the ways and steps of making variable arguments in the last video.
We have made one average function where we can take the average of three numbers, by providing arguments we can even get an average of five numbers.
In this video we will be discussing memory management.
We will see how we can manage the memory used by variables or strings or arrays or any other type of storage in which we make the utilisation of the memory, we use the memory.
So, how can this memory be efficiently used? This is what we'll be seeing in this video.
How the memory can be dynamically allocated.
All these things we will be discussing in this video.
So, without any delay we will start and see, why to manage memory.
This memory that is there with us why we have to manage it? A scenario where we have no idea about the size of our text, in this situation we have to define a pointer to character without defining how much memory is required.
And later based on the requirement we can allocate the memory.
So, basically what is this entire definition trying to stay? In such situations where we don't know exactly how much text we have to write or we have to put.
In that situation what we do usually is, we make a pointer on one character, which means on the starting point we made a starting character and in front of the character, the things start getting attached and what happens in this, our requirement which is there, how much of space we required, basis that, then we allocate the memory.
So here if we do this thing, if we don't know how much memory is to be used, we blindfolded will allocate 100 memory and then what will happen? Finally, if our text comes in 5 sizes, suppose.
Then what will we do? Five sizes will be stored in it but the rest of the 95 storage space which was there will remain unused and it will go to waste.
So, to reduce the wastage and to manage the memory efficiently, for that we use various types of functions.
Because of which we can do memory management easily.
So, the C language provides us with many functions that deal with the allocation and management of the memory.
So, where should the memory be allocated? For how much size, how much should be the allocation, how should be the management.
For all this we have different types of functions in C programming by default.
so we will see all of these functions and we will use them practically as well.
So, we will see all these functions, which are all there.
So, we will move ahead and see which are the functions that have been used over here for the allocation of the memory.
You can see that the “calloc” named function is being used over here, in which there are two arguments and both are of integer type.
We pass the number in one and in the second one we pass the size or the size requirement that is there.
In the same way we also have free functions, then there is malloc function, re-alloc function.
So, these different functions define how to allot and how much to allot.
These things are defined here.
So, all these we will explore then we will get confused between all these.
So, for the start, we will focus on one function and we will use it practically, basically we will understand how the memory can be managed efficiently.
After that, going ahead we can practice the rest of the functions.
So, we will go ahead and straightaway go towards the practical.
So we will go to our Visual Studio Code.
And here with a simple program we will understand how we can manage our memory efficiently.
So, what we have done here, in this program, is basically two types of storage.
In one storage what we have done, when we don't know how much storage space will be coming to us.
At that time, blindfolded we wrote an estimate storage and based on that estimate storage.
We are adding the text.
What we have done in another case, we have done dynamic memory allocation because of dynamic memory allocation what has happened? How much text was incoming, based on that the storage allocation happened and there was no wastage of space.
So, how is it working, we will go and see.
So, first comes our preprocessor command which is standard input output dot h, after that comes “string dot h” because we will be handling strings as well, then we have seen that standard LIB dot H file which is a header file, it is also being used here.
The standard library one header file, basically we will use few functions like this malloc function, it has been imported for that over here.
So, these are our preprocessor commands and after this our main function starts and you can see that here two types of variables are defined.
The first is there is a string named CH and we don't know how many characters or text we can store in the string.
So, to handle that situation at the start we have declared the size as 50.
If we have declared the size 50 and our text is of only 5 size.
So, the rest of the 45 will remain unused, it will be wasted.
So, this situation will be in the first scenario, if we talk about the second scenario so, here we have given pointer to the character and we have named it DESC. (read the letters separately)
And with the help of this DESC, we can allocate the memory dynamically with the help of malloc function.
What will happen after using this malloc function? Whatever was the requirement of text, only that much storage space will be allocated.
So, these are the two situations or two scenarios now, we will see how we can make out the difference between these two.
So, we will start with the very first string, which is the ch string and we have copied the text in it, Learnvern.
So, we have copied learnvern in ch string with the help of string copy function.
If we talk about the other line, what have we done here? Here we have dynamically allocated the memory and how have we allocated, whichever is our character that size multiplied it by 200.
So, in this way we have allocated the memory dynamically and how is it working? We will see over here.
So, if this desc pointer which we have declared earlier, if its value is null which means if there is nothing stored in it what will happen then, we will pass one printf statement and with the help of that printf statement what are we basically printing? We will be printing here unable to allocate required memory.
So, what happened in this since our memory had become null over here so, with the help of STDERR function we got this printed over here as the memory is not getting located over here.
But if it so happens that here desc null doesn’t come, which means there is some value that has been stored in it.
In that case what will we do? In that case, we will run a string copy function and in the string copy function in desc, we will store this particular string.
So, whatever is the string size, that size string will be made here and our memory will be used only that much, there will be no unused memory which will be wasted over here.
That's why we are saying that with the help of this particular function.
We can allocate our memory dynamically and we'll be able to reduce the wastage of space.
So, we have discussed these two scenarios: in one we have blind folded, declare the Space Storage and the other one based on text we have defined, what should be the string size.
So, after that what have we done basically in the topic.
In the topic we have printed the first string which is ch and in the description we have printed desc, which was our pointer, the value inside it we have printed here in the description.
So, you can see here, both were different ways of storing in the memory.
But the more efficient one was the second method in which we have used the malloc function.
So, we will quickly run it and for that we will go here in the terminal.
And first of all, we will clear this and after clearing it.
Now we will type gcc and then malloc dot c, which means we will write our files name and we will enter.
So, then our compiler will compile this after that to run it we only have to write the file’s name.
We don't have to give the extension and as soon as we press enter, you can see that here again that problem is arising.
So, to counter this problem what we will do is we will directly right click and run the code and as soon as we will run this code you can see that we have got the output with us.
So, what is the output saying, “Topic is equal to learnvern” and “Description is equal to this is a C programming course by Learnvern”.
So, in this way, we have stored two different strings.
We have stored them in two different ways, in one there was wastage of space.
And in the other one dynamically the memory was allocated because of which the wastage was reduced.
So, in this way we can use the malloc function, along with that if we talk about the other functions, which are calloc, free or re-alloc functions, so we can use them in different situations and scenarios.
If we practise them, we will get to know how to efficiently use memory management.
And we will be able to use memory efficiently.
Now after seeing the memory management.
Time comes for the next topic which is called exploring the command line arguments.
So, what we call the command line which means the terminal that we use or the console that we use.
In which our input or output has been printed, even if we want to put any arguments, like we were giving arguments in function.
In that command line or in the terminal line we want to provide any arguments.
How we will provide it, we will be discussing this in the next video.
Thank you so much.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.