« Dynamic memory allocation

Understand dynamic memory allocation

Why dynamic memory allocation?

Whenever we allocate memories there are two types of memories stack and heap. Heap is bigger than stack. Whenever program starts it start with a stack memory.

1int arr1[20];
2
3int n;
4cin>>n;
5int arr2[n];

This works on most of the compilers but we should not do this. C++ doesn't guarantee that this will work on all systems or compilers. We should not create a variable size array. At compile time it should be clear what is the size of the array.

1int i = 10;
2int a[20];

These are pretty small and can be easily handled in stack.

1int a[20000];

This is huge but as this is known at compile time so system makes the stack that big so that program can accommodate this big data in stack.

Problem comes when we do like this:

1int n;
2cin>>n;
3int arr[n];

System is not aware how big stack it needs to start with and it can not increase stack after starting the program. If n is way too big then, stack might not be able to accommodate arr[n]. For stack we decide at compile time. We call this static allocation as its already decided. For heap we decide at run time. We call this dynamic allocation.

dynamic_memory.jpg

Dynamic memory allocation

1int i = 10; // in stack
2int *p = new int; // in heap

new int alone is useless as it is returning address to make it useful we need to store it. And pointers are the variables that store the address

1int *p = new int;

Here 4 bytes for new int is created in heap memory while 8 bytes are allocated in stack memory for pointer p.

dynamic_memory_storage.jpg

Dynamic array

1int *pa = new int[50];
2// 8 bytes in the stack and 200 bytes in the heap.
3
4int n;
5cin >> n;
6int *pa2 = new int[n];
7// this is fine now and in heap.

Here pa will take 8 bytes in stack and 200 bytes in the heap. As we are using heap so now we don't need to be aware whats the size of the array we need, we can ask at run time. Here pa we can use exactly as array like pa[2].

De-allocating dynamic memory

1main{
2 if(){
3 int i=10;
4 }
5}

Here i will be auto cleared after this if block

1while (true)
2 {
3 int k = 10;
4 int *p = new int;
5 }

Here with each iteration k's 4 bytes is cleared and new 4 bytes are allocated. But in case of pointer, pointer's 8 bytes will be cleared with each iteration but 4 bytes of new int will remain. Its there waiting that you might use. Dynamic allocated memory does't have a scope or name.

1int *p = new int;
2delete p; // single element deletion
3
4
5p = new int;
6delete p;
7p = new int[8];
8delete[] p; // array deletion

After usage is complete call delete p then heap memory will be released. We don't need to worry about pointer's 8 bytes as it will cleared after scope is finished. After delete is called don't use the memory as its not ours now. p was not deleted but the memory pointed by p was deleted so we again do something like p = new int;.

Difference between static and dynamic memory allocation

Main difference between static and dynamic memory allocation is that static memory will be automatically released based on the scope while in case of dynamic allocation, manual release is required.

static_vs_dynamic_memory.jpg

Final Code

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12
13using namespace std;
14int main()
15{
16 int i = 10; // in stack
17 int *p = new int; // in heap
18 delete p;
19 p = new int; // single element deletion
20
21 delete p;
22 p = new int[8];
23 delete[] p; // array deletion
24
25 *p = 10;
26 cout << (*p) << endl;
27
28 double *pd = new double;
29 char *c = new char;
30
31 int *pa = new int[50];
32 // 8 bytes in the stack and 200 bytes in the heap.
33
34 int n;
35 cin >> n;
36 int *pa2 = new int[n];
37
38 return 0;
39}