Page 1 of 1

Quick delete question

Posted: Mon Nov 03, 2008 5:45 pm
by mrmarky2
I have two integers that are pointers. **cages2 and *cagenums.
I want them to have different size arrays they are pointing to.
cagestotal is another integer, that changes as well.
When I use new to make them it looks like this:

Code: Select all

cages2 = new int *[cagestotal];
cagenums = new int [cagestotal];
And later on when I have filled cagenums with numbers, I make the cages2[] to have the same size as the integer stored in cagenums[]:

Code: Select all

for(int i=0; i<cagestotal; i++)
{
   cages2[i] = new int [cagenums[i]];
}
This gets me the arrays of the size I want.
But.. when I want to change the size of the arrays and start again, I tried using this code:

Code: Select all

delete [] cages2;
delete cagenums;
But this seems to crash my program.
When I replace this code with the code below it works:

Code: Select all

for(int i=0; i<cagestotal; i++)
{
   delete cages2[i];
}
If I put

Code: Select all

delete cages2
at the end of this then it crashes too.
Do I need to use delete like this so that I can remake the arrays to a different size?

Thanks
Mark

Posted: Mon Nov 03, 2008 6:01 pm
by mrmarky2
And the error message:

An unhandled exception occurred. Press "Abort" to terminate the program, "Retry" to exit the program normally and "Ignore" to try to continue.

Posted: Mon Nov 03, 2008 9:34 pm
by Auria
Why do you create an array of pointers to ints? Wouldn't a regular array work?

BTW if you need to change the size of the array, why don't you use std::vector? It will save you a lot of trouble.

Posted: Tue Nov 04, 2008 6:41 am
by vsp
This is a general c++ question.

This will not work
delete [] cages2;
delete cagenums;
How ever this should work fine
for(int i=0; i<cagestotal; i++)
{
delete cages2;
}



If it crashes, you are messing up with pointers.

Posted: Tue Nov 04, 2008 2:29 pm
by mrmarky2
Why do you create an array of pointers to ints? Wouldn't a regular array work?
Um, because I dont know what size the regular arrays need to be and because each of the pointers are pointing to different size arrays.
For example:

Code: Select all

int **cage;
cage = new int *[5];
cage[0] = new int [6];
cage[1] = new int [7];
cage[2] = new int [4];
cage[3] = new int [2];
cage[4] = new int [3];
They only way I could see how to do this is arrays of pointers.

The only problem before of using vectors is I would need an vector of vectors, where each of the vectors in the array are of different sizes. I'm trying to replace my code now with vectors.. i'll let you know if theres any problems.

Thanks
Mark

Posted: Tue Nov 04, 2008 2:59 pm
by mrmarky2
Haha, got it working with vectors.
Thanks for the suggestion. I'm gonna rewrite a load of the code in the next version, vectors would have been really helpful earlier on. I always found a way of getting round it before :)

Mark

Posted: Tue Nov 04, 2008 3:45 pm
by Disch
I know this is solved and you're using vectors now, but I felt I should mention this anyway:

Since you were using new[] for all items, you should use delete[] for all, too. vsp's code suggestion had the right idea, but he made a somewhat critical mistake:

Code: Select all

for(int i=0; i<cagestotal; i++)
{
delete[] cages2[i];  // delete[]!  not delete
}
Don't mix new/delete with new[]/delete[].