SEARCH THIS BLOG

MEMORY ALLOCATION IN C++

Static & Dynamic Allocation of Memory

Memory allocation in C++

The golden rule of computers states that anything and everything (i.e. data and instructions) that needs to be processed must be loaded in the memory before its processing takes place. Therefore, every data and instruction that is being executed must be allocated some area in the main (internal) memory. This allocation can be done in two ways:


Static Memory Allocation

Dynamic Memory Allocation

 

Static Memory Allocation :

When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as static memory allocation.

When the compiler compiles the written code, it provides adequate memory to all the statically defined variables and objects. The compiler knows the space taken by a particular variable by its datatype.

For example, consider we declare an int variable as follows:

         int value;

As soon as the compiler executes this statement, it provides a random 4 bytes of space in the internal memory and names that location as ‘value’.

This variable remains in the memory for its entire life time and then gets automatically deleted from the memory once the variable goes out of scope.

 

Dynamic Memory Allocation :

When the amount of memory to be allocated is not known beforehand and the memory is required to be allocated as and when required during the execution of the program(i.e. during runtime), then, the allocation of memory is referred to as dynamic memory allocation.


C++ offers two operators for dynamic memory allocation, new and delete.


The operator new allocates the memory dynamically and returns a pointer storing the address of the freshly created memory.


General syntax for a variable creation using new operator is :

                        int *a=new int;

                        *a=23;


Here, the first statement creates a pointer to integer variable and then allocates 4 bytes of space in the memory (say at address 400). So ‘a’ is a pointer that points to a memory block of size 4 bytes (sizeof(int)) and that memory block has address 400.

 

The second statement uses the dereferencing operator (*). ‘*a’ reads as “value at a” and is a way to access the value stored in the memory block with the address stored in the pointer ‘a’ (i.e. the value at position 400). It then sets the value as 23.

 

The operator delete deallocates the memory (reverse of new) pointed by the given pointer. This is important as the memory allocated dynamically using new doesn’t get deleted when its scope is over. It remains in the memory for the entire execution of program and thus wastes the memory which could otherwise be used for other allocations. For a short program it may not seem a problem but for programs using thousands of dynamic variables, it may cause crashing of the program due to memory shortage. Therefore, it is compulsory to delete a variable created through new as soon as its job is over.


Syntax for delete is simple :     

delete pointer_name;

 

Read all about Dynamic Allocation Operators here.

Download the differences between static and dynamic memory allocation.

 

 

Free Store 

Every program is provided with a pool of unallocated heap memory that it may utilise during execution. This pool of available memory is referred to as the program’s free store.

 

Free Store is a pool of unallocated heap memory given to a program that is used by the program for dynamic memory allocation during execution.

 

The allocated free store memory is unnamed. Objects allocated on the free store are manipulated indirectly through pointers. Another aspect of free store is that the allocated memory is uninitialized. The programmer is responsible for initializing it explicitly.

 

The free store memory is allocated through new operator and deallocated through delete operator.

 

Free store memory is (dynamically) allocated during run-time and static memory allocation takes place during compile-time. An object’s life time, i.e. as long as the object remains in the memory during the program execution, is known as an object’s extent.

 

Global variables are said to have static extent. That means they are allocated statically during compile time and remain in the memory throughout the program’s execution.


Variables having local scope are said to have local extent. Storage is allocated to them at each entry into the local scope(i.e. as soon as the local scope starts); and on exit(from the local scope), the storage is freed. (note : A local variable with static specifier has static extent).


Objects that are dynamically created using new are said to have dynamic extent. The storage allocated through the use of operator new remains bound to the object until it is explicitly deallocated by the programmer using delete.

 

 

 

 


Do not stop here. Read all articles related to C++ here.

If you have any doubts, queries or suggestions for the website, please comment on this post.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

Download the pdf form of this article here.


Related Topics :

Introduction to pointers

Pointer Arithmetic

Dynamic Allocation Operators

Memory Leaks

Pointers and Constants



Post a Comment

Comments

Loading... Logging you in...
  • Logged in as
There are no comments posted yet. Be the first one!

Post a new comment

Comments by