C++ Tutorial 9 – Dynamic Memory

Hits: 0

C++ Tutorial 9 – [Dynamic Memory]

Learning is fortunate.

1. What is dynamic memory

In C++ programming, [memory] can be divided into two parts, the stack and the heap.
Stack: All variables declared inside a function use stack space.
Heap: It is all the unused space in the program and can be used for dynamic allocation.
Dynamic memory allocation is possible using specific operators.
Dynamic memory allocation can be achieved using the new operator.
Use the delete operator to delete previously allocated dynamic memory.

2. Implementation method

Dynamic memory allocation can be achieved in C++ in the following ways.

type_name* element= new class_name;

Taking int as an example, the specific application method is:

int* test = new int;
*int = 5;

In C++, previously allocated dynamic memory can be deleted as follows.

delete element;

Taking int as an example, the specific application method is:

delete test;

3. Application examples

3.1. Dynamic memory allocation of single variables and objects

The following example describes how to implement dynamic memory allocation for single variables and objects.

#include <iostream>

using namespace std;

class  Shape 
{ 
public :
     // Constructor definition 
    Shape( double l = 2.0 , double b = 2.0 )
    {
        length = l;
        breadth = b;
    }
    // destructor definition
    ~Shape() 
    {
        cout << "The class is end" << endl;
    }
    void setLength(double lengthIn) {
        length = lengthIn;
    }
    void setBreadth(double breadthIn) {
        breadth = breadthIn;
    }
    virtual double getArea() = 0;

protected :
     double length;      // length 
    double breadth;     // width
};

// Circle 
class  Circle : public Shape {
 public :
    Circle(double l = 2.0, double b = 2.0)
    {
        length = l;
        breadth = b;
    }
    double getArea() {
        return 3.1415926*length*breadth / 4;
    }
};

// Rectangle 
class  Rectangle : public Shape {
 public :
    Rectangle(double l = 2.0, double b = 2.0)
    {
        length = l;
        breadth = b;
    }
    double getArea() {
        return length * breadth;
    }
};

int  main ( void )
 {  
     // single variable part 
    int * test = new  int ;
    *test = 5;

    cout << "Dynamic memory allocation for single variable" << endl ;
     cout << "The int is " << *test << endl ;
     cout << endl ;

    // object part 
    Circle* circle = new Circle( 2 , 2 );
    Rectangle* rectangle = new Rectangle(3,4);

    cout << "The area of circle is " << circle->getArea() << endl;
    cout << "The area of rectangle is " << rectangle->getArea() << endl;

    // delete the previously defined dynamic memory 
    delete test;
     delete circle;
     delete rectangle;
    system("pause");
    return 0;

}

The result of the application is:

Dynamic memory allocation for single variable
The int is 5

Dynamic memory allocation for objects
The area of ​​circle is  3.14159 
The area of ​​rectangle is  12 
The class  is  end 
The class  is  end 
Press any key to continue . . .

3.2. Dynamic memory allocation of arrays

3.2.1. Dynamic memory allocation of [one-dimensional arrays]

#include <iostream>
using namespace std;

int main(void)
{   
    int* arr = new int[5];
    for (int i = 0; i < 5; i++) {
        arr[i] = i;
    }
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    delete[] arr;
    system("pause");
}

The result of the application is:

0  1 2 3 4 
Press any key to continue .  . .

3.2.2. Dynamic memory allocation of [two-dimensional arrays]

#include <iostream>
using namespace std;

int  main ( void )
 {  
     // dynamic memory allocation 
    int ** arr;
    arr = new int*[5];
    for (int i = 0; i < 5; i++) {
        arr[i] = new int[4];
    }

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 4; j++) {
            arr[i][j] = i * 5 + j;
        }       
    }
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 4; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    // delete dynamic memory 
    for ( int i = 0 ; i < 5 ; i++) {
         delete [] arr[i];
    }
    delete[] arr;
    system("pause");
}

The result of the application is:

0  1 2 3 
5  6 7 8 
10  11 12 13 
15  16 17 18 
20  21 22 23 
Press any key to continue .  . .

3.2.3. Dynamic memory allocation of three-dimensional arrays

#include <iostream>
using namespace std;

int  main ( void )
 {  
     // dynamic memory allocation 
    int *** arr;
    arr = new int**[2];
    for (int i = 0; i < 2; i++) {
        arr[i] = new int*[3];
        for (int j = 0; j < 3; j++) {
            arr[i][j] = new int[4];
        }
    }

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                arr[i][j][k] = i * 3 * 4 + j * 4 + k;
            }
        }       
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                cout << arr[i][j][k] << " ";
            }
        }
        cout << endl;
    }

    // delete dynamic memory 
    for ( int i = 0 ; i < 2 ; i++) {
         for ( int j = 0 ; j < 3 ; j++) {
             delete [] arr[i][j];
        }
        delete[] arr[i];
    }
    delete[] arr;
    system("pause");
}

The result of the application is:

0  1 2 3 4 5 6 7 8 9 10 11 
12  13 14 15 16 17 18 19 20 21 22 23 
Press any key to continue .  . .

You may also like...

Leave a Reply

Your email address will not be published.