C++ Dynamic Memory

Dynamic and static there are two type of memory allocation. there are most of programming language are support this features. Static memory are related to stack area. And dynamic memory are related to heap. And this memory are specific roles and task.

Static memory allocation is also known as automatic memory allocation. This memory are used by compiler is internally. When compiler is execute any user defined function including main function. Then the compiler are create memory of all function and its local variables using stack area. and end of function execution they are automatically free associative memory. This is internally process and compiler are do this job. Recursion is one of the best example. When function call are execute recursively then this process are work.

Dynamic memory is utility in hand of programmer. programmer are created (allocate) , destroy (free) dynamic memory of heap area. In C++ new and delete operator are used to this task. This are provide flexibility to programmer. most of data structure are suitable and reliable to use dynamic memory. there are complete control of programmer to those memory. linked list is one of the example.

How to create dynamic memory in c++?

Help of new operator we can request to dynamic memory on heap area. If memory are available then it will return the address of this memory. if other case memory of not available then it will return NULL. interesting factor heap area is also limited it depends our system structures.

data_type *ptr_name=new data_type;
/*
  data_type: data type (predefined and inbuilt)
  ptr_variable :  pointer variable name
  new :keyword
*/
//for example
int *ptr=new int;
float *auxiliary=new float;

In this example new are request to create dynamic memory of two variables integer and float. If memory allocation are successful created then it will return the valid memory location address. otherwise return NULL. see the example how to use those variable in our program.

/*
Example create dynamic memory of c++ variable
*/
#include <iostream> 
using namespace std; 
int main(){
  /*
  create pointer variable and request
  for dynamic memory of integer variable
  */
  int *ptr=new int;
  /*check pointer variable is null that 
    means not allocated memory of heap area*/
  if(ptr==NULL){
    //not allocated memory
    cout<<"memory overflow"<<endl;
  }else{
    // created memory is successfully
    *ptr=90; //provide value
    cout<<*ptr; //access the value
  }
  return 0;
}
Output
90

In this program new are return the address of one integer there are created on heap area. and assign this address to pointer variable. help of pointer we are insert and get the value of this dynamic memory. first of all we are check if memory is allocated or not. this is good programmer habit. Note that there are used to statements. first create memory using this statement " int *ptr=new int;". and after that assigned the value on this memory location using this statement "*ptr=90;". we can do similar task in single statements. view example.

/*
Example create dynamic memory of c++ variable
*/
#include <iostream> 
using namespace std; 
int main(){
  /*
  create pointer variable and request
  for dynamic memory of integer variable.
  if memory are created then assign this location
  of  integer value (90).
  */
  int *ptr=new int(90);
  /*check pointer variable is null that 
    means not allocated memory of heap area*/
  if(ptr==NULL){
    //not allocated memory
    cout<<"memory overflow"<<endl;
  }else{
    // created memory is successfully
    cout<<*ptr; //access the value
  }
  return 0;
}
Output
90

In this example are create only one integer variable. we can create any number of inbuilt and user defined data on heap area. but only memory are available in heap area.

Create dynamic array in c++.

Array are contain block of memory in given size. we can Allocate block of memory using this syntax.

pointer_variable=new data_type[size];

for example we are create an array of 10 integers of dynamic. see the example

/*
  Example create dynamic array in c++
*/
#include <iostream> 
using namespace std; 
int main(){
    //create array of 10 integers
  int *array=new int[10];

  if(array!=NULL){
    int index;
    //assign value
    for(index=0;index<10;index++){
      array[index]=(index+1)*2;
    }
    //display data
    for(index=0;index<10;index++){
      cout<<"array["<<index<<"] : "<<array[index]<<endl;
    }
    //free the memeory
    delete array;
    //assign pointer to NULL
    array=NULL;
  }else{
    cout<<"Memory overflow";
  }
return 0;
}
Output
array[0] : 2
array[1] : 4
array[2] : 6
array[3] : 8
array[4] : 10
array[5] : 12
array[6] : 14
array[7] : 16
array[8] : 18
array[9] : 20

There is programmer responsibility to free the memory of created by new operator. delete are do this task. see the syntax.


 delete pointer_variable; //normal variable
 delete []pointer_variable; //array in this case

view another example and make 2d dynamic array of c++.

#include<iostream>
using namespace std;
int main(){
  //suppose create 2d array of like this array[5][3]  
  //first create an array of 5 integer pointer
  int **array=new int*[5];

  if(array!=NULL){
    int i,j;
    for(i=0;i<5;i++){
      /*create another integer array and assign 
      This address of already created pointers
      */
      array[i]=new int[3];
      //check memory are allocated or not
      if(array[i]!=NULL){
        for(j=0;j<3;j++){
          //assign value
          array[i][j]=i;
        }
      }else{
        cout<<"memory overflow"<<endl;
        exit(1); //exist program
        
      }
    }
    //display result
    for(i=0;i<5;i++){

      for(j=0;j<3;j++){
        cout<<"array["<<i<<"]["<<j<<"] :"<<array[i][j]<<endl;
      }
      //free internal array
      delete [] array[i];
      array[i]=NULL;
    }
    //delete first created array
    delete [] array;

  }else{
    cout<<"memory overflow"<<endl;
  }
  return 0;
}
Output
array[0][0] :0
array[0][1] :0
array[0][2] :0
array[1][0] :1
array[1][1] :1
array[1][2] :1
array[2][0] :2
array[2][1] :2
array[2][2] :2
array[3][0] :3
array[3][1] :3
array[3][2] :3
array[4][0] :4
array[4][1] :4
array[4][2] :4

© 2019, regularcodes.com, All rights reserved