Dynamic memory allocation in C++

Dynamic memory allocation:-

  • In C, we can allocate dynamic memory by using malloc, calloc ande realloc library functions.
  • All the above functions are applicable in C++.
  • But there are some disadvantages in C++.
  • To avoid those disadvantages C++ provides its own memory allocation and de-allocation mechanism
  • For dynamic memory allocation in C++ we have to use ‘new’ operator and to de-allocate that memory we have to use ‘delete’ operator.
  • When we allocate a memory by using ‘new’ then we can not de-allocate a memory by using free().
  • ‘new’ operator internally calls malloc and delete operator internally calls free for memory allocation and de-allocation respectively.
  • malloc function returns void* as a return value but new operator returns an address of appropriate type that’s why there is no type casting required for ‘new’ operator.
  • When we allocate a memory for particular object by using malloc in C++, memory is allocated successfully but it is not initialize with appropriate values.
  • Means when we allocate a memory by malloc constructor for that class is not called.
  • Similarly for when we can call free function, memory is de-allocated but destructor of that object is not called.
  • When we create an object by using ‘new’, memory for that object is allocated on heap.
  • Difference between a normal object creation by using ‘new’ is that when we create an object in normal way like demo d; then that object is destroyed when scope of that statement is terminates.
  • But on other side when we allocate a memory by ’new’, termination of scope of that object is depend on the programmer or when the program terminates, means when we call ‘delete’, scope of that object is explicitly terminates.
  • When we allocate a memory by malloc we cannot provide parameters for that object but when we allocate a memory by ‘new’ we can provide a parameters.

e.g.

class demo

{

public:

int i,j;

demo()

{

i=10;

j=10;

cout<<”inside empty constructor.”;

}

demo(int x,int y)

{

i=x;

j=y;

cout<<”inside parameterized constructor.”;

}

~demo()

{

cout<<”inside the destructor.”;

}

};

int main()

{

demo *d1,*d2;

d1=(demo*)malloc(sizeof(demo)); // no output

d2new demo(); // inside empty constructor.

demp *d3;

d3=new demo(30,40); // inside parameterized constructor.

cout<<d2->i; // 10

cout<<d2->j; // 10

cout<<d3->i; // 30

cout<<d3->j; // 40

cout<<d1->i; // 0

cout<<d1->j; // 0

free(d1); // no output, because, no destructor is called.

free(d2); // error

delete(d2); // inside the destructor.

delete(d3); // inside the destructor.

}

Dynamic memory allocation

Allocate a memory for array by ‘new’:-

  • Consider the syntax,

demo *d=new (demo[10]);

Output:- inside the default constructor (10 times)

  • In this case, we are allocating a memory for 10 objects of a class demo.

De-allocation of memory which is allocated for an array:-

  • When we write a syntax like,

delete(d2);

  • In this case, memory is de-allocated for only 1st object but remaining 10 objects are as it is means this is the disadvantages of delete for de-allocation of an array.
  • To avoid this disadvantages we can use a syntax a,

delete[] d;

  • In this case, all the 10 objects are freed and
  • destructors of all 10 objects are called.
  • Above scenario indicates, writing [] empty square brackets for de-allocation a memory for an array of objects.
  • Mentioning a dimension is optional.
  • When we use above syntax without using [] destructor for 1st object is called and other 9 objects memory is considered as memory leak.
  • The [] indicating that this is not a single object but this is an array of an objects.
  • Also, there is no re-allocation of a memory is possible in C++ like a realloc in C.

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply