Dynamic Memory Allocation in C++

Here in this page we will perform C++ dynamic memory allocation for these following, 


Dynamically allocate Memory Array

The array variable may be a good example to understand the C++ dynamic memory allocation. As we know that array is fixed size. That is, when we declare an array variable, we also give it a size in which we can not change at run-time and so it can only store values ​​up to the given size or less.

You can understand this as if we have created a program and stored a record of 5 students using an array variable (by declare array size 5). It can store only 5 student records and if we store less than 5 student records in it, then the remaining memory will be waste which will remain in the program even if not needed.

Meaning if you store less records, then memory will be waste and even if needed, we cannot store more than 5 student records in it.

In such a situation we have to change the array size in the program to store more records, which will not be considered the right way.

But in the dynamic memory allocation, we can increase and decrease the size of the array according to our requirement at run-time. Thus, there will not be any kind of memory waste here.

But remember that the run-time allocate memory is required to release from the delete operator.

SYNTEX:

To allocate memory,

data-type pointer-variable = new data-type[size];

To de-locate memory

delete [size]pointer-name;

Here is the Example,

#include<iostream.h>
#include<conio.h>

void main()
 {
  int i,size;
  int *ptr;
  clrscr();

  cout<<"Enter Array Size: ";
  cin>>size;

  ptr=new int[size];

  cout<<"\nEnter "<<size<<" Element in Array: ";

   for(i=0;i<size;i++)
    {
       cin>>*(ptr+i); //  here ptr[i]; will also work 
    }

  cout<<"Values Are\n";

   for(i=0;i<size;i++)
    {
       cout<<ptr[i]<<"\t"; // (ptr+i); will also work
    }

  delete[] ptr;    // delocating memory located by new operator

  getch();
}
OUTPUT:

1st execution:-
Enter Array Size: 3
Enter 3 Element in Array: 1 2 3

Values Are
1 2 3

2nd execution:-
Enter Array Size: 6
Enter 6 Element in Array: 1 2 3 4 5 6

Values Are
1	2	3	4	5	6

explanation;

In 1st execution 3 element is stored and array variable will allocate memory for only 3 element.

          In 2nd execution 6 element is stored and array variable will allocate memory for only 6 element.

     In this way we can see how dynamic memory allocation is important.

sometimes maybe happen that array size depend on user input but in large code it occur bugs.

Dynamically create object for structure and class in C++

Yes, it is possible to create variable in structure and class and its useful when we work with array and object.

SYNTEX:-

Because, class and structure are almost the same, hence dynamic memory allocation in both is done in the same way.

structure-name pointer-variable = new class-name
structure-name pointer-variable = new structure-name;

For more read this similarities and different between structure and class.

Here is the program, where we used structure

#include <stdio.h>
#include <string.h>
#include<conio.h>
#include<iostream.h>

struct test
 {
   int x,y,sum;
   void get_sum(int,int);
 };

void test::get_sum(int a, int b)
{
   x = a;
   y = b;

   sum = x+y;
   cout<<x<<" + "<<y<<" = "<<sum;
}

void main()
{
   clrscr();

   test *ptr = new test; // dynamic object created 
   ptr->get_sum(3,4);    // member function calling

  delete ptr; // releasing memory using delete operator

  getch();
}
OUTPUT:-
3 + 4 = 7

previous-Pointer in C++

next- Function and their type | Parameter type