Dynamic Memory Allocation In C++

Dynamic Allocation is means by which a program obtain memory during runtime.
The local and global variables are allocated memory during compile time (at the time of compiling our program).
What if, our program needs to use variable amount of memory.
For example, if we need an array in our program but we don’t know of what size (say for instance that size is accepted by user at runtime).
In these cases, the dynamic allocation methods can serve our purpose.

Every program is provided some amount of heap memory by the compiler.
It is a pool of unallocated free memory and is used by dynamic allocation methods to obtain memory at runtime.
We’ll now look at some methods of dynamic allocation of memory.

The new operator

This operator can be used to create objects of all types (ints, floats, arrays, structures and classes; we’ll look into that in later chapters).
General syntax is
pointer-variable = new data-type;
where data-type is any valid C++ data type and pointer-variable is a pointer variable of type data-type.
The operator new will allocate memory of size equal to size of specified data-type and return a pointer (of the same data-type only) pointing to the newly allocated area.

For example, ptr = new int; this will allocate sufficient amount of memory from heap memory provided by compiler to hold a value of int data-type and store the address of the newly allocated memory in pointer variable ptr.
Since new is allocating memory for int type, the receiving pointer must also be of type int.

We can also combine declaration and assignment as,
int *ptr = new int;

Now we can use iptr as normal pointer like,
*iptr = 54;

The newly allocated memory (through new) can also be initialized at the time of allocation itself.
General syntax is,
pointer-variable = new data-type (value);
For example,
int *iptr = new int (54);

Creating Dynamic Array

The new operator can also be used to allocate memory for user-defined types like structures, arrays and classes.
For Example,
int *arr = new int [10];
It will create memory space from the heap memory for an array of 10 integers.
Arrays defined are 0-indexed as usual.

The delete operator

When an object, created through new, is no longer needed, it must be destroyed so that the memory space occupied by it may be released for reuse.
This can be done with the help of delete operator, the memory de-allocation operator in.
General syntax is,
delete ptr;

(ptr is a pointer pointing to a memory space that was allocated by new operator and now is to be deleted)

Note: Memory allocated at compile time cannot be freed up by delete operator, the are only freed once the program’s execution terminates.

The arrays allocated through new are freed as follows,
delete [size] array-pointer-variable
(size can be left omitted)

Example Program

#include<iostream>
using namespace std;

int main()
{
    int *rollNo;
    float *marks;
    int sizeA,i;
    cout<<"Enter number of students: ";
    cin>>sizeA;
    rollNo = new int [sizeA];
    marks = new float [sizeA];

    for(i = 0; i < sizeA; i++)
    {
        cout<<"Enter roll no and marks for student "<<i+1<<" : ";
        cin>>rollNo[i]>>marks[i];
    }
    for(i = 0; i < sizeA; i++)
    {
        cout<<rollNo[i]<<"   "<<marks[i]<<endl;
    }

    delete[] rollNo;
    delete[] marks;
    return 0;
}

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?