Solved

auto_ptr

Posted on 2011-03-10
6
395 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 171
Socket Programming (Unix) 8 142
C++ question 3 70
boost::uuid crashes 17 11
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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