Questions tagged [auto-ptr]

A C++ template class that provides a limited garbage collection facility for pointers, by allowing pointers to have the elements they point to automatically destroyed when the auto_ptr object is itself destroyed. Deprecated as of C++11 in favor of unique_ptr.

auto_ptr is deprecated as of C++11. For the C++11 equivalent, unique_ptr, see the reference page.

Reference page for auto_ptr.

197 questions
2
votes
2 answers

Auto Pointer Issue

I'm new to C++ and a bit confused regarding auto_ptr. I have a class which inside has a static auto_ptr. static std::auto_ptr con = std::auto_ptr (util::getDBConnection() ); Util::getDBConnection()…
Rudy
  • 7,008
  • 12
  • 50
  • 85
2
votes
1 answer

What is the danger of passing an auto_ptr to a function expecting a constant reference to an auto_ptr?

Nicolai Josuttis, in his book "The C++ Standard Library - A Tutorial and Reference", writes, at page 44, the following paragraph : According to the concept of auto_ptrs, it is possible to transfer ownership into a function by using a constant…
Belloc
  • 6,318
  • 3
  • 22
  • 52
2
votes
2 answers

I believe there is a typo in Stroustup's book, third edition page 368. Could someone confirm?

I believe there's a typo on this code snippet extracted from Stroustup's book, at its page 368 : template class std::auto_ptr { template struct auto_ptr_ref { /* ... */ }; // helper class X * ptr; public : typedef…
WaldB
  • 1,261
  • 7
  • 14
2
votes
2 answers

Example of C++ std::vector> that compiles but fails

What would be a simple C++ program where a std::vector> compiles but fails to execute correctly, whereas the same program with std::vector> compiles and works correctly, for some data type T? I know that…
Mark Meretzky
  • 89
  • 1
  • 6
2
votes
1 answer

c++ auto_ptr destroyed when passed into a function

Suppose that we have void UsePointer (auto_ptr spObj) { spObj->DoSomething(); } and we have a main function: int main() { auto_ptr spObject (new CSomeClass ()); UsePointer (spObject); //…
Lost1
  • 990
  • 1
  • 14
  • 34
2
votes
1 answer

Incomplete type using typedef function pointer

I've got an abstract base class that defines an interface to data sinks. Concrete implementations of data sinks are acquired via factories. In an effort to tidy up code, I created a typedef for the factory method that returns new DataSink objects…
Ryan Talbot
  • 115
  • 1
  • 6
2
votes
6 answers

how this auto_ptr program works and what it does?

I ran this program but I didn't get what this auto_ptr does and on which basics it shows the values? int main(int argc,char **argv) { int *i= new int; auto_ptr x(i); auto_ptry; y=x; count <
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
2
votes
3 answers

Providing a "safe" push() function for use with auto_ptr

I want to declare a "safe" push() function for use with auto_ptr like this: template inline void push( StackType &s, auto_ptr p ) { s.push( p.get() ); p.release(); } I also want it to work for null pointers,…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
2
votes
2 answers

Using auto_ptr object

I need to store my error and log messages to the file. This is the code sample: #include #include #include int main() { std::auto_ptr cerrFile; try { std::ofstream* a; a = new…
Yury Finchenko
  • 1,035
  • 13
  • 19
2
votes
1 answer

Passing an auto_ptr as an argument to a constructor

I want to be able to pass an auto_ptr as an argument to a constructor. But if the new object could not be created (probably bcoz of no memory), then I want the original auto_ptr to be able to retain its value. Eg: foo (Auto_ptr autop1) { …
2
votes
2 answers

auto_ptr with swig

I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here. But I can't get it to work. Swig generates code that wants…
Gregor
  • 21
  • 1
2
votes
3 answers

Warning linking libxml++-2.6 (c++11 obsoltes std::auto_ptr). Shall I just ignore it?

Using gcc with -std=c++11 pkg-config libxml++-2.6 --modversion 2.40.1 get lots of warnings like this: /usr/include/libxml++-2.6/libxml++/parsers/saxparser.h:224:8: warning: 'template class std::auto_ptr' is deprecated [-Wdeprecated-declarations] I…
Andu
  • 43
  • 6
2
votes
3 answers

Containers of auto pointers

I know containers of auto pointers should not be used and can cause problems. What is the actual reason for that? Is there any other kind of "smart" pointer which is safe to use in a container?
Gianluca
  • 805
  • 1
  • 10
  • 20
2
votes
2 answers

C++ smart pointer for a non-object type?

I'm trying to use smart pointers such as auto_ptr, shared_ptr. However, I don't know how to use it in this situation. CvMemStorage *storage = cvCreateMemStorage(); ... use the pointer ... cvReleaseMemStorage(&storage); I'm not sure, but I think…
Brian
  • 1,663
  • 5
  • 18
  • 26
2
votes
3 answers

Using C++ from Objective C : how to allocate/deallocate?

Currently, my Objective C classes use C++ objects by doing a new when the owner is created, and calling delete when it is destroyed. But is there another way? I'd like to be able to declare, say, an auto_ptr whose scope lasts the duration of the…
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166