 Hello and welcome to the NPTEL course on an introduction to programming through C++. I am Abhiram Ranade and this lecture is about representing variable length entities and the reading for it is chapter 21 of the textbook. So let me begin with the programming problem. So we would like to design a scheme to store the names of the students in your class. Now there is a natural solution to this, use a two dimensional array like we talked very recently, store the ith name in the ith row. Now what will the row size have to be? So the row size will have to be as large as the length of the longest name and therefore it seems that most rows will be mostly empty and therefore we are using the memory quite inefficiently. So you might wonder is there a better scheme and this is not the only problem that we are looking at today indirectly, there are also other problems which are very similar. So for example for some reason I might want to store a whole bunch of polygons in my program and say the polygons have varying number of sides. So again the provision that I might have to make might be for the largest polygon whereas I might end up having a lot of small polygons. So again I will end up wasting a lot of space. So the C++ standard library which is discussed in chapter 22 of the book and which we will take up later on contains very safe and convenient classes for this. And they will be sufficient to store names, polygons and other such entities and you should use them wherever possible. But in this lecture or in this chapter of the book we are going to talk about how to build the classes like those in chapter 22. So essentially we are going to understand what is the mechanism behind these classes. The sort of what is the magic on which these classes run. So here is the outline for today's lecture. So I am going to talk about something called the heap memory which is something that you have not seen so far. And there will be a discussion of the primitives for allocation and deallocation of memory from this heap memory. Then we will talk about how do you manage this heap memory. And we will do a detailed example. And in this example we will talk about a class for representing text strings. And this will be useful in storing the names of students, the problem with which we started this lecture. So let me begin with what you already know. So you already know the activation frame memory. So how do we define variables? Well so far the idea has been that the programmer will give the variable definitions in the text of the main program or of some function as well. And the memory for these variables will be allocated when control reaches the variable definition statements. And the memory will be allocated in the activation frame of the current function. And it will also be deallocated, well it will be deallocated when the control exits the block containing the definition. So it could be exiting the function, it could be a for statement whose variables whose control variable might be getting deallocated but yes the variable will be deallocated as well and there is a very clear point where the variable is going to get deallocated. Now variables which are created and destroyed in this way are called automatic variables. Automatic because the memory allocation and deallocation sort of happens automatically. Well I mean you could say that the user is saying user is defining the variable and therefore it is getting created yes but at least the deallocation is sort of I mean it is indicated by the end of the block. But anyway so because the user is not explicitly asking for memory at least the user is not explicitly giving up memory these things are called automatic variables. In addition to the activation frames we also have this so called heap memory. So this is a completely separate reserved region of memory completely different from the activation frames and in this memory it is possible to explicitly request say give me some memory for a certain variable and that memory will be given from the heap and we will be saying that this variable has been allocated in the heap or on the heap whatever and when there is no more use for the allocated memory the program or maybe the programmer must explicitly return the memory to the heap. So the program there must be a statement in the program which will say now I do not need this memory take it back put it back and maybe give it to me later if I ask for it again. So after the memory is returned it can be used to satisfy other memory allocation requests that might come up in the future. Now as you can see there are explicit statements which say that look give me this memory or there will be an explicit statement which says take this back and so therefore this memory is not automatic in any sense and even the de-allocation is explicit it does not happen just because a block is exceeded by the control. So what have we discussed so far well we already find the problem the problem being how to efficiently use memory for storing entities with varying sizes and we have said that the solution will be provided using the heap memory which is separate from the activation frame memory and next we are going to talk about how to use the heap memory but we will take a quick break.