C++ Dynamic Memory

    C++ is a statically typed programming language, but it also supports the concept of Dynamic memory allocation, in dynamic memory allocation the memory allocated to the variable at runtime. Sometime situation could arise where we require dynamic data, that means the data keeps changing during runtime, for example, if we create a program for employee management and here, we continuously require a data set which can add and delete data during runtime.

    What is Memory Allocation?

    When we declare or define a variable or any function in a program there are two ways via which memory can be allocated to these both.

    • Compile Time and Static Memory Allocation
    • Runtime and Dynamic Memory Allocation

    Compile Time Allocation

    In this memory allocated to the variables and functions before the program gets executed and here the compiler allocates the fixed amount of memory to the program, so throughout the program execution, the program occupies the fixed amount memory. By far in all the examples we have seen the static or compile-time memory allocation. In Static memory allocation, the compiler uses the stack memory.

    Runtime or Dynamic Memory Allocation

    In Dynamic Memory Allocation, the memory allocates to the program variables and functions at runtime. In Dynamic memory allocation heap memory comes in use, here the compiler has no idea about the exact size of the program and the program space can be altered during the run time.

    Stack Memory

    All the functions and variables of the program use the stack memory.

    Heap Memory

    It is the unused memory of the program and if there is dynamic data then only the program uses the heap memory to store that dynamic data.

    Dynamic Memory Allocation:

    In dynamic memory allocation, we simple allocate storage space at runtime, this does not mean we can define new variables at runtime the dynamic allocation deal with two criteria.

    • Create dynamic space in the memory.
    • Store the memory address in a pointer.

    C++ new and delete Operator

    In C++ we can allocate memory to a variable at runtime within the heap memory using a special operator new. If sufficient memory is available, the new operator initializes the memory and returns the address of the memory allocated and initializes it to a pointer variable. If you have used C programming language you must be aware of malloc() function which acts similar to new operator, but in C++ we are suggested to use new rather than malloc() because new doesn't just allocate memory, it constructs objects which is a prime concept of C++ Deallocation of the dynamic memory is also a part of dynamic memory allocation and if we do not require the dynamically allocated memory anymore, we can simply remove that part of the memory from our program using the delete operator. new and delete syntax

    pointer_variable = new data_type;
    
    delete data_type;

    The data_type could be either built-in or user-defined data types such as int, double, float, array, structure, class, etc. Example

    #include <iostream>
    using namespace std;
    
    int main () {
       int* ptr_var  = NULL;
       ptr_var  = new int;  
       *ptr_var = 201343;    
       cout << "Value of ptr_var is : " << *ptr_var << endl;
       delete ptr_var;         // free memory.
       return 0;
    }

    Output

    Value of ptr_var is : 201343

    In dynamic memory allocation, the compiler does not destroy the object itself so it’s the responsibility of the programmer to use the delete operator to free memory.

    Dynamic Memory Allocation for Array

    Using the new operator, we can allocate dynamic memory to the array variables. syntax

    pointer_variable = new data_type[array_size];

    Example

    char* val  = NULL;      
    val = new char[40];

    Dynamic Allocation of Class objects:

    #include <iostream>
    using namespace std;
    class emp {
    public:
        emp()
        {
            cout << "Constructor Used" << endl;
        }
        ~emp()
        {
            cout << "Destructor Used" << endl;
        }
    };
    
    int main()
    {
        emp* E = new emp[6];
        delete[] E;
    }
    Output:
    Constructor Used 
    Constructor Used 
    Constructor Used 
    Constructor Used 
    Constructor Used 
    Constructor Used 
    Destructor Used 
    Destructor Used 
    Destructor Used 
    Destructor Used 
    Destructor Used 
    Destructor Used

    People are also reading: