Dynamic memory allocation
The concept of memory was used on and off in these first two lessons to explain some fundamental concepts in C++. The main aspect of using memory in previous lectures was getting values from and storing values in specific places in the memory. The main topic in this lecture on the other hand will be different types of memory, getting some space in those memories for our use and also releasing unused memory.
Memory allocation
Any time a program is run on the computer, a certain chunk of memory is given to the program for its use. Additionally, if the program requires more memory, it could ask the computer for more. The process of getting new parts of the memory for use is called memory allocation. The main question you may have at this point is: Why do we need to allocate memory? Can’t we just set the pointer to a random address and use that? The answer is no.
The computer (specifically the operating system) is responsible for keeping the memory intact. That means that if everyone had access to every single location in the memory, there is a possibility of two programs using the same memory location. In that case, there could be two scenarios:
- One program overwriting some crucial data which the second program stored
- One program reading some data stored by the other which shouldn’t be read
For that reason, every single block in memory has certain privileges – imagine them as access codes and only the program with that access code can access the block of memory.
Stack and Heap
When running a program, the operating system grants the program a chunk of memory it needs. Before the program starts, it tells the operating system how much memory it needs to begin with. That part of memory is called the stack. It’s a part of memory which stores all the defined variables in the program. That means that all of the variables we have defined so far (all the arrays) are stored on the stack.
Now, there may come a point where we cannot know in advance how much memory space we need. Imagine we want to have an array, but we do not know how many elements it needs to have in advance. At this point, we need to ask the operating system to give us a certain chunk of memory. That chunk will be given in the moment of execution of the program. In contrast, this type of memory now cannot be stored on the stack, but is instead stored in a part in memory called the heap. It’s purpose is to store data which wasn’t allocated in advance, but instead during the program’s execution.
Let’s look at a simple example. Imagine the previous problem where we wanted to have an array of changing size. We would first require that the user inputs the number of elements the array needs to have (for this, we will use another built-in feature of C++) and then allocate enough memory for our array. The following code does just that:
int N;
cin >> N; /* cin is a built-in functionality (like cout) which stores the inputted value in
the variable*/
int * arr = new int[N]; // allocates an int array of size N
The new keyword is used for allocation of memory.
Table of contents
- Classes and Objects
- Operator Overloading
- Class Inheritance
- Exceptions
- Namespaces
- Templates
- Standard Library
- Additional Problems