Jump to content
  • Advertisement

Archived

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

kuphryn

Using auto_ptr :: C++

This topic is 6017 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

Hi. Under what situation should one use auto_ptr instead of plain pointer? I am in a situation where I cannot decide on using auto_ptr or pointer. I like auto_ptr because I trust that if something goes wrong at run-time, auto_ptr will help eliminate possible memory leaks. When do you recommend using auto_ptr? Thanks, Kuphryn P.S. auto_ptr declaration cannot be in a switch: switch (variable) { case 1 : auto_ptr temp(new class); // illegal ... }

Share this post


Link to post
Share on other sites
Advertisement
declare a new epoch and it will let you instance it there.

don''t use auto_ptr if you''re going to copy it alot (as when using the STL containers)

And use CComPtr for COM interfaces, otherwise, use auto_ptr whenever you want.

Share this post


Link to post
Share on other sites
Okay. Thanks.

I think I figure out the essense of auto_ptr. auto_ptr is a quick and easy exception handling for class objects.

Here is a performance question. I always make sure all classes I use are secure from memory leak. I implement exception handling when dealing with many standard library functions that could return errors, i.e. new. Anyways, is it better to use auto_ptr and let the class handles unexpected run-time errors, or is it better to make sure the class is completely error proof (as much as possible)?

Kuphryn

Edited by - kuphryn on December 28, 2001 12:16:27 AM

Share this post


Link to post
Share on other sites
here''s a good article on auto_ptr:
Using auto_ptr effectively

also, pay attention when using auto_ptr, because it can lead to some really weird behaviour, i recommend you take a look at the smart pointer classes at www.boost.org for alternatives

(btw: stl containers of auto_ptr''s should not compile)

Share this post


Link to post
Share on other sites

  • 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!