Quick delete question Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Quick delete question

Post 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
Last edited by mrmarky2 on Mon Nov 03, 2008 6:03 pm, edited 1 time in total.
mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post 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.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post 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.
vsp
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon Feb 21, 2005 12:52 pm

Post 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.
mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post 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
mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post 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
User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Post 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[].
Post Reply