Jump to content
  • Advertisement
Sign in to follow this  
Dko

Is there an error in this?

This topic is 4723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was looking thru my learning c++ in 21 days book and think I found a very very bad error :3 I just wanted to make shure to see if my knowlage on pointers is correct. Ill just put in the main func. The class isn't really important/ int main() { CAT * Family[500]; int i; CAT * pCat; for (i = 0; i < 500; i++) { pCat = new CAT; pCat->SetAge(2 * i + 1); Family = pCat; //wtf where is the delete statement for all these created cats } for (i = 0; i < 500; i++) { cout << "Cat #" << i+1 << ": "; cout << Family->GetAge() << endl; } return 0 } Isn't this bad? Isn't there saposta be a delete for every new? Because if thats so new is being called 500 times and that means 500 * worth of memmory the size of a cat is created and never freed. >.< THus major boo boos

Share this post


Link to post
Share on other sites
Advertisement
Yes delete/delete[] should be called, but it aren't that bad, because almost all compilers (or is it the OS?) release all still allocated memory of a program when it exits.

Share this post


Link to post
Share on other sites
Yes I think I remmberd that. But I wanted to make shure and even the book says to always have delete even in that case. ^^ lol Jesse liberty should follow his own advice :P Not trying to be mean but "but it aren't that bad" isn't gramatically correct(I know I suck at English my self.) But should be more like "But it isn't that bad". k? ^^

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
Yes delete/delete[] should be called, but it aren't that bad, because almost all compilers (or is it the OS?) release all still allocated memory of a program when it exits.


That's the OS, but it's still a bad idea. First, you're not guaranteed that this will happen (as it depends solely on the OS), and second, in real life the program might not exit immediately after delete should have been called, meaning that you'll eat up memory that can't be freed until the program exits. ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Quote:
Original post by CTar
Yes delete/delete[] should be called, but it aren't that bad, because almost all compilers (or is it the OS?) release all still allocated memory of a program when it exits.


That's the OS, but it's still a bad idea. First, you're not guaranteed that this will happen (as it depends solely on the OS), and second, in real life the program might not exit immediately after delete should have been called, meaning that you'll eat up memory that can't be freed until the program exits. ;)


Yes I know it's a bad idea, and it shouldn't be done this way, especially not in a beginner book. I'm just saying that it's much better than if you allocate a lot of objects at start of an application, don't use them throughout the application and they are deallocated when the application exits.

Quote:

Yes, there should be a third for() loop in which the cats are deleted.

Or you could just change the second to this:


for (i = 0; i < 500; i++)
{
cout << "Cat #" << i+1 << ": ";
cout << Family->GetAge() << endl;
delete Cat;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!