Memory Allocation in C++

Sanket Manmode
Dev Genius
Published in
3 min readJul 31, 2020

--

Static Memory Allocation and Dynamic Memory Allocation in C++.

Each variable uses space on computer’s memory to store its value.

When we use the term allocate, we indicate that the variable has been given a space on computer’s memory.

Deallocations means the space has been reclaimed by computer and the variable cannot be accessed now.

This article explains how this memory is allocated to variables in C++ programming language.

Memory is divided into two parts. First is called Stack memory and other is Heap memory.

Stack memory store variables declared inside function call and is generally smaller than heap memory.

Heap memory is used in dynamic memory allocation and is generally larger than stack memory.

Memory allocation in C++ is done by two methods.

One of them is Static Memory Allocation which is also called as Compile Time Allocation.

And the other one is called as Dynamic Memory Allocation which is also know as Run Time Allocation.

Static Memory Allocation

In static memory allocation, size and location where variable will be stored is fixed during compile time.

Let us see how static memory allocation takes place using following code.

In the above code there are three variables, x is a integer variable, ch is a character variable and a is a integer array.

In static memory allocation, compiler calculates how much memory these variables will need and fix a location where these variables will be stored.

Imagine a symbol table like following,

a — -> 0

ch —-> 401

x — ->402

Using table like this, locations of where variables will be stored is saved. But, actual physical memory is not allocated to the variable at compile time.

Actual physical memory is allocated only at runtime.

Some other important point about static memory allocation are as follows:

  1. Static memory allocation is slightly faster than dynamic memory allocation.
  2. This type of memory allocation is less flexible.
  3. Allocation and deallocation of memory is done by compiler itself.

Dynamic Memory Allocation

Dynamic memory allocation allows you to define memory requirement during execution of the program.

Dynamic memory allocation uses special type of memory called as heap memory. Do not confuse this with heap data structure.

In dynamic memory allocation, new keyword is used to allocate memory and delete keyword is used to deallocate memory.

Unlike static memory allocation, allocation and deallocation of memory should be done by the programmer. That means programmer need to write code for allocation and deallocation the memory.

The new keyword returns the address that has been allocated to the variable on the heap memory. This happens during execution of the program.

Let us see how memory is allocated dynamically during runtime using following code.

In the above code, there is a variables n which is a integer variable and arr which is a integer pointer. Both of these variables are stored in the static part of the memory.

But during execution of the program, depending on the value of n, new keyword returns the physical address of the memory where the array has been allocated memory on the heap.

Please read the above statement carefully to understand the working of the program.

arr points to the address where array is stored. The address in question here is the one returned by new keyword.

Some other important points to notice about dynamic memory allocation are as follows:

  1. The advantage of dynamic memory allocation is that reuse of memory is possible.
  2. Memory leak is possible. Programmer needs to be careful while re-assigning memory to another variable.
  3. Compiler does not help with allocation and deallocation. Programmer needs to both allocate and deallocate the memory.

This was memory allocation in C++. Thank you for reading and I hope you had a good read. Have a nice one!

--

--