Jump to content
  • Advertisement
Sign in to follow this  
ControlDenied

delete method

This topic is 5144 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 have seen in C++ FAQ Lite the following way to delete a multidimensional array allocated with new
for (unsigned i = nrows; i > 0; --i)
    delete[] matrix[i-1];
delete[] matrix; 


Why not
for (unsigned i = 0; i < nrows; ++i)
    delete[] matrix;
delete[] matrix; 


? I have always used the second way, never had a problem except once that throwed an unhandled exception. With the first way it did not. Does anybody knows why? Also, why --i or ++i instead of i-- or i++?

Share this post


Link to post
Share on other sites
Advertisement
Both methods should work, the only difference is that you count up, the other code counts down.

It is good practice to use preincrement/decrement instead of postin/decrement. It doesn't matter when you use integers, but when you are incrementing iterators ++i will be faster then i++ because i++ has to create a copy.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by ControlDeniedAlso, why --i or ++i instead of i-- or i++?


incidently C++ FAQ LITE explains that as well

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!