Solved

auto_ptr

Posted on 2011-03-10
6
392 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
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 33

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 53

Expert Comment

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

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 121
VS2015 compilation and missing DLLs 9 148
C++ question 3 67
VS2015 Redefinition errors 4 53
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

831 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