Dynamic memory allocation of objects in C ++

I am trying to dynamically allocate (it is not as dynamic as it is now, but in the end it will be) the memory for objects in a simple program is very . I am new to classes and only recently started playing in C ++, leaving C behind. Here is 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 :( */ } 

Most of my confusion is actually included in comments in the code itself. I am basically trying to have an array c , where each element of the array is an object of itself.

The code behavior that I get is described in the comments.

+4
source share
5 answers

Maybe we should take a look at the ads expanded by you:

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

You defined b and c as a pointer to the test, but you seem to want c to be an array of the pointer to the test. An application for c you intended, probably:

 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 will access in this way:

 c[0]->print(); c[1]->print(); 
+5
source

There are several serious issues with this code.

  • Doing new on *b , but skipped to delete .
  • You overwrite *c several times in the for loop that the Memory will leak. Always release resources before allocating a new one from a pointer.
  • If you highlight new/new[]/malloc , then you should free the pointer with delete/delete[]/free respectively. the same that you do not support with *c (why it does not work).

In addition to studying dynamic allocation, you should also know the STL containers that provide the best way to handle dynamic resources. e.g. std :: vector .

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

The above memory leak code. c simply points to the last constructed object in the loop iteration.

C-> print (); / * does this show that the value i = 1 .. should be 0?

Here c points to the location built on new Test(1); . So, the way out.

Each new [] must be followed by a delete [] and a new one with delete . You cannot mix both.

0
source

That delete[] does not work is perfectly normal: you never allocated c as an array, but as a pointer. You can save the address of the array in a pointer, but that’s it. I'm really curious why exactly c [1] works, because your for loop simply stores multiple pointers to newly selected objects in the same pointer (you are not filling the array!).

0
source

delete c[]; Deletes only the start element. If you want to delete this array, use dz delete c[] for the loop

You were unable to allocate memory for c and continue to encode it incorrectly, how can you get the output without allocating memory to a pointer variable?

0
source

All Articles