Worked fine until I put in the deallocation stuff at the bottom. Now it crashes. WTF?Code:int **loop;
.
.
.
loop = new int* [size];
.
.
.
for (i = 0; i < size; i++)
loop [i] = new int [size + 1];
.
.
.
for (i = 0; i < size; i++)
delete loop [i];
Printable View
Worked fine until I put in the deallocation stuff at the bottom. Now it crashes. WTF?Code:int **loop;
.
.
.
loop = new int* [size];
.
.
.
for (i = 0; i < size; i++)
loop [i] = new int [size + 1];
.
.
.
for (i = 0; i < size; i++)
delete loop [i];
Check the the [] operator.
delete [] data[nIndex];
Kuphryn
just nitpicking: when you allocate memory, you don't exactly own it... you're asking the Operating system to lend you the memory for a while... The operating system can always take your memory away if it decides you're using it wrong. :D
You need to check to see if what you are deleting is an array. When you use:
new []
to create an array, then you use
delete []
to delete the array. Since every loop[i] is an array, you need to:
delete [] loop[i];
You could have some multidimensional array:
loop[i][j][k][l]
and if each element of that array were a dynamically allocated array itself, then you would
delete [] loop[i][j[k][l];
Still doesn't work. I'll just keep taking more and more memory without giving it back until the OS falls over. It's obviously what MS want or else they wouldn't release a compiler that blatantly disregards your explicit instructions.
So try it on gcc under Unix. :rolleyes:Quote:
It's obviously what MS want or else they wouldn't release a compiler that blatantly disregards your explicit instructions.
This compiles and runs fine in VC6:
The destructor is called 6 times, which is what you would expect for a 2x3 array.Code:#include<iostream>
using namespace std;
class Apple
{
public:
~Apple()
{
cout<<"destructor called"<<endl;
}
};
int main()
{
int size = 2;
Apple **loop;
loop = new Apple* [size];
for (int i = 0; i < size; i++)
loop [i] = new Apple [size + 1];
for (i = 0; i < size; i++)
delete [] loop [i];
return 0;
}
Then I don't know. It just falls over on mine...
the following worked in Dev-C++ (two slight modifcications)Code:#include<iostream>
using namespace std;
class Apple
{
public:
~Apple()
{
cout<<"destructor called"<<endl;
}
};
int main()
{
int size = 2;
Apple **loop;
loop = new Apple* [size];
for (int i = 0; i < size; i++)
loop [i] = new Apple [size + 1];
for (int i = 0; i < size; i++) //added int in front of the first i
delete [] loop [i];
std::cin.get(); //put this in to wait for user input
return 0;
}
You haven't really showed us your code yet, so you might want to consider posting it. Did you cut and paste the code I posted exactly as I posted it and try to run it, but it didn't work?
Is it just me, or has everyone forgotten to delete[] the array of pointers?
Code:for (int i = 0; i < size; i++) //added int in front of the first i
delete [] loop [i];
delete[] loop;
Found the problem - the program didn't necessarily need all the arrays. Every iteration of the main loop, a condition would possibly cause the program to break out of the loop if it was prematurely complete (a possibility with this algorithm) but afterwards it would attempt to delete ALL arrays (even those that weren't allocated due to premature exit).Quote:
Originally Posted by 7stud
:rolleyes:
It was late.... ;)
Ooops.Quote:
Is it just me, or has everyone forgotten to delete[] the array of pointers?
:confused:Quote:
Originally Posted by me
Yes, my code does that but I didn't bother putting it up here as it wasn't relevant to my problem.Quote:
Originally Posted by Hunter2
Ah, dontcha just hate dynamic memory..Quote:
a condition would possibly cause the program to break out of the loop if it was prematurely complete (a possibility with this algorithm) but afterwards it would attempt to delete ALL arrays
*upbeat music begins*
Use std::vector! :)