Solved

auto_ptr

Posted on 2011-03-10
6
403 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 35099768
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
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 35105896
0
 
LVL 34

Expert Comment

by:sarabande
ID: 35107392
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 35107436
Heh, I don't think that's the point of the question, Sara ;)
0
 
LVL 34

Expert Comment

by:sarabande
ID: 35107509
sorry, i always overact when i see pointers :)

Sara
0
 

Author Closing Comment

by:learningunix
ID: 35130688
got it
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question