In this article static memory allocation we give the information about the memory allocated by static memory allocation is de-located or freed when the program terminates.

Memory allocation in C++

To store user input, we declare variables in the program. These inputs take up space in memory according to their data type at compile time, which cannot be changed at runtime but by using dynamic memory, we can manipulate the memory of these inputs at run time.

  • The memory taken by a variable remains in the program till the end of program execution, although the task for which the variable was used is over
  • Since they allocate memory at compile time, we cannot change their size at runtime, that means their size remains stable.
  • Dynamic memory gives us the facility, in which we can take the memory (up to a limit) as per our requirement at runtime and can also free it when the need is over.

Type of memory allocation in C++:-

Memory allocation is the most important concept in C++. Memory is allocated in two ways in C++ :–

  1. Static memory allocation
  2. Dynamic memory allocation

Static memory allocation in C++:-

  • Static memory allocation is by default.
  • In this memory allocation happens at compile time. That’s why it is also called compile time memory allocation.
  • Because the memory size is fixed in it, so it is also called static memory allocation.
  • The memory allocated by static memory allocation is de-located or freed when the program terminates.
  • It does not have any syntax. That is, when we declare any variable in the program. So automatic allocates space in memory itself. So in a way, the normal declaration of a variable is an example of a static memory allocation-

int a;  char b; int arr[5];

These data types will take up space in memory according to their size in the following way-

a = 2 bytes;

b = 1 byte

In other words,

arr[5] = 10 bytes // 2 byte x 5 = 10

name[10] = 10 bytes // 1 byte x 10 = 10

So in this way the static memory allocation memory is automatic (at compile time) memory and automatically de-locate (when the program terminates). The programs given in this tutorial so far are examples of static memory allocation memory.

Dynamic memory allocation in C++:-

  • Allocating memory at runtime is called dynamic memory allocation.
  • In Dynamic memory allocation, we can change the memory size at runtime (not the default size of any data -type) and release it when the need is over.
  • Note here that if we allocate memory dynamically then .manually it has to be de-allocated too. Failure to do so may result in the problem of memory leakage in the program.
  • Such static memory is not generated in the allocation because there the memory is de-allocated automatically.

Because here the memory allocation happens at runtime, so the memory size is not known at compile time, that is, at runtime it depends on the requirement of the user that how much memory is to be allocated.

Note:

dynamic memory allocation. In this we do not declare any variable separately at runtime. Only increase the memory size of the already declared variable which depends on the data type.

Allocating dynamic memory in C++

In C++, dynamic memory is allocated using the new– operator. In this memory allocation (by new -operator) and de-location (by delete operator), both are done manually by the programmer.

Because here we allocate memory according to our requirement in runtime when required, so dynamic memory allocation provides flexibility to the program.

Its syntax is given below –

SYNTAX

data-type pointer-variable = new data-type;

The pointer -variable is used to hold the address of dynamically allocated memory, dynamic memory is accessed from this pointer -variable. Here the data type can be built-in -data type or user defined data type.

new operator in C++:-

In dynamic memory allocation, first we declare pointer variable, which stores the address of dynamic memory. Note that the memory allocation will depend on the data type.

For example-

int *p = new int; // allocate 2 byte Then, assign the address of new int to *p.

p = new int;

Then the value is stored using the pointer-variable *p.

*p = 5;

These multiple statements can also be declared in a single statement –

int *p = new int(5);

delete operator in C++:

As mentioned above, it is necessary to de-locate dynamic memory, for which delete operator is used, its syntax is given below-

SYNTAX

delete pointer-variable;

Here, the pointer variable will contain only the name of the pointer variable i.e. the asterisk (*) will not be used with the pointer variable in memory de-location.

An example of this is given below –allocate dynamic memory for int data type

#include<conio.h>

#include<iostream.h>

 void main()

 {

  clrscr();

  int *p = new int; // memory allocated at runtime

  *p = 5; //assign value into *p

  cout<<*p; //print

  delete p; // memory release

  getch();

  }

OUTPUT

5

Explanation:-

In the program, a dynamic memory of type int is allocated from the new operator. Whose address is stored in a pointer variable p* and in the next statement the value 5 is stored in that memory.

This value stored in memory is accessed from p*. After the result is received, the dynamic memory is also de-located by the delete operator.

Note here that the size of dynamic memory here will be only 2 bytes because we have taken int data type whose memory size is 2 bytes.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *