Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic memory allocation of objects in C++

I'm trying to dynamically allocate (it's not so dynamic as it is right now, but eventually it will be) memory for objects in a very simple C++ program. I'm new to classes and have only recently started playing with C++, leaving C behind. Here's the code:

#include <iostream>
using namespace std;

class Test {
  private:
    int i;
  public:
    Test(int);
    ~Test();
    void print();
};

Test::Test(int ii) { i = ii; }
Test::~Test() { i=0; cout << "deconstructor called...value of i= " << i << endl; }
void Test::print() { cout << "value of i= " << i << endl; }

int main()
{
  Test a(10),*b,*c;
  //a.print(); // this works

  b = new Test(12);
  //b->print(); // this works as well

  for (int i=0; i<2; i++)
    c = new Test(i);

  c->print(); /* this shows that the value of i=1 .. should be 0? */
  c[0].print(); /* as expected (I guess), this prints i=1 as well... [expected because c->print() shows i=1 also */
  c[1].print(); /* shows value of i=0... */

  //delete []c; /* this fails miserably, but `delete c` works, why :( */

}

A lot of my confusion is actually included within comments in the code itself. I'm basically trying to have an array c where each element of the array is an object of itself.

The behavior of the code that I'm getting is described in the comments.

like image 271
Amit Avatar asked Jul 25 '11 03:07

Amit


People also ask

Can we allocate memory for objects dynamically?

We can also dynamically allocate objects. As we know that Constructor a special class member function used to initialize an object and Destructor is also a class member function which is called whenever the object goes out of scope. Destructor is can be used to release the memory assigned to the object.

Is there dynamic memory allocation in C?

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size.

What is dynamic memory allocation in C explain with example?

The Dynamic memory allocation enables the C programmers to allocate memory at runtime. The different functions that we used to allocate memory dynamically at run time are − malloc () − allocates a block of memory in bytes at runtime. calloc () − allocating continuous blocks of memory at run time.

How memory is allocated to an object in C?

You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator.


2 Answers

Perhaps we should look at the declarations, expanded you have:

Test a(10);
Test *b;
Test *c;

You have defined b and c as pointer-to-Test, but you seem to want c to be an array of pointer-to-test. The declaration for c you intended was likely:

Test **c;

which you would initialize:

c = new Test*[2];

for (int i=0; i<2; i++)
   c[i] = new Test(i);

and which you would access thus:

c[0]->print();
c[1]->print();
like image 178
Dave Avatar answered Sep 30 '22 23:09

Dave


There are few serious problems with the given code.

  1. Performing new on *b but missed to delete it.
  2. You are overwriting *c few times in for loop, which will leak memory. Always deallocate resources before allocating a new one from a pointer.
  3. If you are allocating with new/new[]/malloc then you must deallocate the pointer with delete/delete[]/free respectively. The same you are not maintaining with *c (that's why it fails).

Also, apart from learning dynamic allocation one should also be aware of STL containers, which provide a better way of handling dynamic resources. e.g. std::vector.

like image 40
iammilind Avatar answered Sep 30 '22 21:09

iammilind