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
1 answer

Crashes and leaks with std::auto_ptr despite type is full qualified in destructor

I read C++: auto_ptr + forward declaration? and its answers, especially the accepted one and I'm aware of the pitfalls when combining auto_ptr and forward declarated classes. But I'm encountering runtime problems with this same pattern that seems…
Wolf
  • 9,679
  • 7
  • 62
  • 108
2
votes
1 answer

Combining auto_ptr with references in C++

Some time ago, I decided strictly following the rule to check each pointer before dereferencing it the first time in a scope, I also changed pointers to references where appropriate: in some cases statically in the code base, in some cases…
Wolf
  • 9,679
  • 7
  • 62
  • 108
2
votes
2 answers

Auto Pointer in C++ (auto_ptr)

I am trying to figure out what this piece of code prints but I couldn't output it for some reason, it gave me an error: "1 [main] Q1c 5752 open_stackdumpfile: Dumping stack trace to Q1c.exe.stackdump". double *dp=new double(1.2); auto_ptr
Khalifa Bader
  • 69
  • 2
  • 9
2
votes
3 answers

Function taking a std::auto_ptr that can accept std::auto_ptr

I am trying to create a function that takes an auto_ptr to Base class and I would like to call it with a auto_ptr to Derived class. However I am failing to get it done. I have tried using it without a reference: void function(std::auto_ptr
rozina
  • 4,120
  • 27
  • 49
2
votes
1 answer

std::auto_ptr compiles in my template class but not std::unique_ptr

I started a template class that is supposed to manage a fixed length deque. I was looking to add a function that would return the data transformed into a vector. Because I cannot be sure this will be compiled with good NRVO (named return value…
crogg01
  • 2,446
  • 15
  • 35
2
votes
1 answer

Is returning auto_ptr from functions wrong/error-prone?

Let's say I'm using std::auto_ptr in my code.* Is there any danger in returning an std::auto_ptr object? i.e. Could it result in a memory leak, undefined behavior, etc.? or is it a safe use of std::auto_ptr? *I'm not asking if there is a better…
user541686
  • 205,094
  • 128
  • 528
  • 886
2
votes
3 answers

Boost Python callback returning auto_ptr deletes the object

I'm creating bindings for a 3rd party library that takes ownership of objects so I'm trying to use auto_ptr as documented in the FAQ. Here's an example of two classes I've wrapped: typedef std::auto_ptr PanelAutoPtr; class NewPanelCallback…
Jonatan
  • 3,752
  • 4
  • 36
  • 47
2
votes
1 answer

smart pointer - what if constructor throws?

I have a class that connects to a USB device in the constructor. If the device isn't present or some other situation fails then the constructor throws an exception and the calling code deals with it. Something akin to: CDevice* pDevice =…
ethrbunny
  • 10,379
  • 9
  • 69
  • 131
2
votes
2 answers

how can I use auto_ptr as member variable that handles another member variable

I have a class like this: class A { private: B* ptr; } But B ptr is shared among different A objects. How can I use auto_ptr so that when A gets destructed B stays on so that other A objects that point to the same ptr can continue without…
VNarasimhaM
  • 1,460
  • 3
  • 22
  • 36
2
votes
1 answer

function wont accept iterator to auto_ptr

I wrote some flawed Huff compression code that I was trying to fix. The first thing I did was to switch the pointers to auto_ptr (There are reasons I didn't use another smart pointer). I create a vector of auto_ptr but when I try to pass an auto_ptr…
sinθ
  • 11,093
  • 25
  • 85
  • 121
2
votes
2 answers

Returning multiple auto_ptrs from a function

I have a function that allocates two variables on the heap and returns them to the caller. Something like this: void Create1(Obj** obj1, Obj** obj2) { *obj1 = new Obj; *obj2 = new Obj; } Usually, in similar cases, when I have a function…
FireAphis
  • 6,650
  • 8
  • 42
  • 63
2
votes
1 answer

How do you assign a returned auto_ptr?

I'm trying to learn auto_ptr, so I wrote the code below but it results with ..\src\main.cpp:23: error: no match for 'operator=' in 'p1 = source()()' What have I done wrong? How do you assign a returned auto_ptr? #include #include…
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
2
votes
1 answer

making auto_ptr to a friend class

This is a sample code of my project. I have to make the std::auto_ptr to a friend class, so that it can access private members. #include "stdafx.h" #include #include //sample namespace namespace test { //class A …
Aneesh Narayanan
  • 3,220
  • 11
  • 31
  • 48
2
votes
1 answer

Segmentation fault in auto_ptr.release()

I get a segmentation fault at this point of code when calling auto_ptr release member: try { newMod->init(params); } catch (const std::exception& e) { #ifndef CONFIG_STATIC dlclose(handle); #endif throw…
Zbigh1
  • 379
  • 4
  • 13
1
vote
3 answers

ISO C++ forbids declaration of 'auto_ptr' with no type

I'm trying to write a small application and have come across a compile time error using auto_ptr. I originally tired creating a smart pointer with class I created but the same error occurs if I try and create a smart pointer of type int so there…
Tommy
  • 591
  • 2
  • 11
  • 21