Sign in to follow this  

delete method

This topic is 4838 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[i];
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
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 [url=http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.12]C++ FAQ LITE[/url] explains that as well

Share this post


Link to post
Share on other sites

This topic is 4838 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this