1)How can i delete the memory allocated if i have allocated dynamically through "new" like that:
classA** instances;
instances=new classA*[5];
for(int i=0;i<5;i++){
instances=new classA;}

2)How can i pass arguments on the constructor while i am allocating memory in this way?:
classB* instances;
instances=new classB[5]

Recommended Answers

All 5 Replies

delete them in the opposite order that they were allocated. use delete inside the loop, but delete[] to delete the array itself.

2) Not possible. You should create another method which can be called after the memory is allocated.

Sry i was trying to quote from this answer and i accidentally gave -1 at the answer..Can u write the de-allocation of memory in 1 in code pls??

Again sry for the -1...

Yes I could, but give it a try yourself. Its easy to do, just write the loop first and after the loop delete the array. If you wrote that code snippet to allocate the memory then I'm confident you can write the code to delete it.

>Sry i was trying to quote from this answer and i accidentally gave -1 at the answer.
I fixed it for you. Now it's back at 0. :)

>Can u write the de-allocation of memory in 1 in code pls??

// Allocate
T **p = new T*[ROWS];

for ( int i = 0; i < ROWS; i++ )
  p[i] = new T[COLS];

// Release
for ( int i = 0; i < ROWS; i++ )
  delete[] p[i];

delete[] p;

thanks guys problem soveld

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.