22 Nov 2019

  • November 22, 2019
  • Amitraj
Dynamic memory (Runtime) Allocation and Deallocation operator


-> Programmers can dynamically allocate storage space while the program is running, but programmers cannot create new variable names "on the fly", and for this reason, dynamic allocation requires two criteria:

1. Creating the dynamic space in memory
2. Storing its address in a pointer (so that space can be accessed)

-> Memory de-allocation is also a part of this concept where the "clean-up" of space is done for variables or other data storage. It is the job of the programmer to de-allocate dynamically created space. For de-allocating dynamic memory, we use the delete operator. In other words, dynamic memory Allocation refers to performing memory management for dynamic memory allocation manually.


*Memory in your C++ program is divided into two parts:

stack: All variables declared inside any function takes up memory from the stack.

heap: It is the unused memory of the program and can be used to dynamically allocate the memory at runtime.


-> C++ defined two new operator name "new" and "delete" to perform task of Allocating and freeing the memory in a better and easier way. since, these operators manipulate memory on the free store. In C language "malloc and calloc and free" are function, whereaas "new and delete" are operators in C++.


// C++ program to demonstrate dynamic memory Allocation and deallocation operators.


#include<iostream>
using namespace std;
int main()
{
         int n;
         cout<<"Enter total no. of students:";
         cin>>n;
         float *marks;
 // dynamic memory allocation / Runtime 
         marks= new float[n];
         cout<<"Enter marks:";
         for(int i=0;i<n;i++)
    {
         cin>>*(marks+i);      // cin>>marks[i];
     }
        for(int i=0;i<n;i++)
     {
         cout<<*(marks+i)<<"\t";
     }
       delete []marks;
        return 0;
}


OUTPUT:

Enter total no. of students:3
Enter marks: 44.6   88.61    36.33
44.6    88.61   36.33


         

Related Posts:

  • C++ Variable Declaration -> C++ is a strongly-typed language, and requires every variable to be declared with its type before its first use. -> This informs the compiler the size to reserve in memory for the variable and how to interpret its v… Read More
  • Structure of a C++ Program Structure of a C++ Program       1.) Header file (Include file) 2.) Member function definition 3.) Class declaration 4.) Main function 1. Header File: In this section, we add header files and resource files… Read More
  • Operators in C++ Operators in C++ C++ has a rich set of Operators. All C operators are valid in C++ also. In addition, C++ introduces some new operators; <<          Insertion operator >>  &nbs… Read More
  • C++ Variable initialization Initialization of variables When the variables in the example above are declared, they have an undetermined or garbage value until they are assigned a value for the first time. But it is possible for a variable to have a sp… Read More
  • C++ Data Types C++ Data Types Data types define the type of data a variable can hold, for example an integer variable can hold integer data, a character type variable can hold character data etc. Data types in C++ are categorised in thre… Read More

Translate

Popular Posts