auto_ptr

Is there any difference betwwen the following declaration

std::string *myStr

str::auto_ptr<std::string> myStr;

Do I have to delete/destory any of these objects or does the compile takes care of it?
learningunixAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
You didn't actually allocate any memory in either those statements, so there's also no need to de-allocate any memory.

However, assuming that you meant these instead :

        std::string* myStr = new std::string();
        std::auto_ptr<std::string> myStr(new std::string());

Then the former would require you to de-allocate the allocated memory yourself (using delete myStr), but the latter will clean up the memory for you automatically, as soon as myStr goes out of scope.

Read more on std::auto_ptr here :

        http://www.cplusplus.com/reference/std/memory/auto_ptr/
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
0
 
sarabandeCommented:
you might not find a reason for using a std::string pointer at all. std::string has dynamic management of internal buffer and reference counting. you won't get a benefit when using pointers rather than objects.

Sara
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Infinity08Commented:
Heh, I don't think that's the point of the question, Sara ;)
0
 
sarabandeCommented:
sorry, i always overact when i see pointers :)

Sara
0
 
learningunixAuthor Commented:
got it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.