• Advertisement

Archived

This topic is now archived and is closed to further replies.

to new or not to new

This topic is 5829 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 through my code and noticed I used new in a few spots where I could just declare the class outright, and let the compiler take care of creation/deletion. So my question is how do people feel about that? Personally, for some reason I like to know when my constructor is getting called for classes, even if the constructor might not do much. I don''t know I just like to know when code is really executing. Of course, it becomes essential to manage the memory carefully to not leave anything hanging, which while not hard when you have clearly defined news, does add a little bit of extra work and introduces areas for potential bugs. Well, I think I''ve actually answered it for myself, but I''m curious what others will say.

Share this post


Link to post
Share on other sites
Static allocation (no news) is usually better than dynamic allocation (news), since you are sure that the object is deleted when it goes out of scope.

To underline that fact, why do you think the C++ Standards Commitee has added auto_ptr<> to the standard library ? To bind memory deallocation to destruction of a local variable. Memory leaks are BAD.

If you are worried about constructors/destructors being called (as you should be), use references instead of passing your data by value.

Share this post


Link to post
Share on other sites

  • Advertisement