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];
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];
Last edited by samGwilliam; 04-04-2005 at 09:07 PM.
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.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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];
Last edited by 7stud; 04-04-2005 at 10:17 PM.
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.
Last edited by samGwilliam; 04-04-2005 at 10:19 PM.
So try it on gcc under Unix.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; }
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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?
Last edited by 7stud; 04-04-2005 at 11:00 PM.
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;
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
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).Originally Posted by 7stud
It was late....
Ooops.Is it just me, or has everyone forgotten to delete[] the array of pointers?
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.Originally Posted by Hunter2
Ah, dontcha just hate dynamic memory..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!
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.