C++ - SPLessons

C++ Memory Allocation

Home > Lesson > Chapter 36
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Memory Allocation

C++ Memory Allocation

shape Introduction

Every programmer must know how to use the memory efficiently. The programs executed till now used variables such that they are declared during the compile time. Those variable remained constant even at run-time. Here is the tutorial describing the memory allocation at run-time. The memory in CPU is divided into two main parts. They are :
  1. stack: Stack stores the variables declared in the functions before the run-time.
  2. heap: Heap is the free memory provided by the operating system to use during the run-time i.e in dynamic manner.

shape Conceptual figure

Static Memory Allocation in C++

shape Description

Static memory allocation involves allocation of memory to the variables during compile-time. The amount of memory wanted can be known before executing the program. The static memory uses stack memory. Stack follows FILO (First In Last Out) structure. When a variable is declared, it is pushed into the stack memory. If there is a need of that variable, it is popped out of stack. 

Dynamic Memory Allocation in C++

shape Description

Sometimes, one may not know how much data is entered by the user and how much memory is required by it. This problem occurs mainly during initialization of variables at run-time. To overcome this, heap is requested to allot some memory to the initialized variables. The pointer moves to the heap memory when dynamic memory allocation is implemented. C++ has special operator called newto allocate memory dynamically.

new

shape Description

Dynamic memory allocation follows two step procedure while allocating memory. The program creates a pointer of any data type and assigns the keyword "new" to it to request the memory space. If the request is accepted, pointer points to the beginning of assigned heap memory. The operator applies only to single element. If array of elements are declared, the operator new[] can be used to request a block of memory.

shape Syntax

//For single element
new data_type;
//For array of elements
new data_type[array_size];

Pointer declaration

shape Description

The pointer must be declared with a data type. So that memory allots space as per the given data type. In case of elements, pointer adds the memory to the previous element. It is better to check the pointer location whether the memory is free or not. So, using of NULL to be made as practice in order to find.
double* ptr = NULL; // Initialization of pointer with null
ptr = new double;> // Request memory for ptr
//Arrays declaration
double * ptr = NULL; // Initialization of pointer with null
ptr = new double[20];// Request memory for ptr array

shape More Info

The primary duty of programmer is to check whether the requested memory is provided or not. It can be done in two ways. [c]int * ptr; ptr = new (nothrow) int [10]; if(ptr== 0) { cout<< " No enough memory" << endl; }[/c]

Memory Deallocation

shape Description

If the work with requested memory is completed, it has to be given back to the operating system to increase the memory efficiency. The memory can be freed up by using the operator delete which is applicable to single elements. If it is applied on arrays, delete removes only the first element in the memory. Arrays can be removed using the delete[]operator.

shape Syntax

//For single elements
delete variable_name;
//For arrays
delete array_name[array_size];

shape Examples

[c] #include <iostream> using namespace std; int main () { int* ptr = NULL; // Initialization with null ptr = new int; // Requesting memory *ptr = 8956; // Storing value at allocated address cout << "The value of ptr is : " << *ptr << endl; delete ptr; // deleting the memory return 0; }[/c] Output [c] The value of ptr is : 8956[/c] Example for allocation of memory to arrays [c] #include <iostream> using namespace std; int main() { int max, count; int* ptr; cout << "What is the maximum number of values? "; cin >> max; ptr = new (nothrow) int[max]; if (ptr == 0) { cout << "Memory is full.Sorry!!"; return 1; } else { for (count=0; count < max; count++) { cout << "Enter any number "; cin >> ptr[count]; } cout << "The list of numbers entered are "; for (count=0; count < max; count++) { cout << ptr[count] << ", "; } delete[] ptr; } return 0; }[/c] Output [c] What is the maximum number of values? 5 Enter any number 1 Enter any number 2 Enter any number 3 Enter any number 4 Enter any number 5 The list of numbers entered are 1, 2, 3, 4, 5,[/c]

Summary

shape Key Points

CPP Memory Allocation chapter draws out following important points.
  • CPP Memory Allocation is done in static and dynamic in which dynamic is preferred.
  • Static works at compile-time and dynamic allocation works at run-time.
  • Memory de-allocation can be done by using delete() function.

shape Programming Tips

Freeing up of memory is preferable to save the memory space.