auto-ptr

Is returning auto_ptr from functions wrong/error-prone?

十年热恋 提交于 2019-12-10 23:53:20
问题 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 substitute (like shared_ptr ); I'm specifically asking about the pitfalls of returning auto_ptr itself. 回答1: In general it's safe and can lead to more robust code. It should not lead to a memory leak since the memory pointed to is automatic deleted.

C++ std::auto_ptr copy constructor

守給你的承諾、 提交于 2019-12-10 23:14:06
问题 std::auto_ptr lacks const copy constructor, therefore I cannot use it directly in collections. is there some way to have for example vector of std::auto_ptr without using boost pointer collection template? 回答1: If you have a C++0x compiler you can use shared_ptr or unique_ptr as appropriate. There is a good example of correct unique_ptr usage here courtesy of @James McNellis. For a shared_ptr walkthrough look here, courtesy of @D.Shawley. [Upvotes would still be appreciated on those threads,

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

送分小仙女□ 提交于 2019-12-10 02:27:00
问题 In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeClass; // OK auto_ptr<NativeClass> mNativeClass; //Not allowed ! boost::shared_ptr<NativeClass> mNativeClass; //Not allowed ! }; Does anyone know of an equivalent of

Using auto_ptr<> with array

喜你入骨 提交于 2019-12-10 00:54:58
问题 I'm using auto_ptr<> which uses an array of class pointer type so how do I assign a value to it. e.g. auto_ptr<class*> arr[10]; How can I assign a value to the arr array? 回答1: You cannot use auto_ptr with array, because it calls delete p , not delete [] p . You want boost::scoped_array or some other boost::smart_array :) 回答2: If you have C++0x (e.g. MSVC10, GCC >= 4.3), I'd strongly advise to use either a std::vector<T> or a std::array<T, n> as your base object type (depending on whether the

std::auto_ptr, delete[] and leaks

本秂侑毒 提交于 2019-12-09 16:47:10
问题 Why this code does not cause memory leaks? int iterCount = 1000; int sizeBig = 100000; for (int i = 0; i < iterCount; i++) { std::auto_ptr<char> buffer(new char[sizeBig]); } WinXP sp2, Compiler : BCB.05.03 回答1: Because you're (un)lucky. auto_ptr calls delete , not delete [] . This is undefined behavior. Try doing something like this and see if you get as lucky: struct Foo { char *bar; Foo(void) : bar(new char[100]) { } ~Foo(void) { delete [] bar; } } int iterCount = 1000; int sizeBig = 100000

Why does this code only print 42?

自作多情 提交于 2019-12-07 03:32:06
问题 Could somebody please explain to me why does this code only print "42" instead of "created\n42"? #include <iostream> #include <string> #include <memory> using namespace std; class MyClass { public: MyClass() {cout<<"created"<<endl;}; int solution() {return 42;} virtual ~MyClass() {}; }; int main(int argc, char *argv[]) { auto_ptr<MyClass> ptr; cout<<ptr->solution()<<endl; return 0; } BTW I tried this code with different values in solution and I always get the "right" value, so it doesn't seem

Is it wrong to use auto_ptr with new char[n]

眉间皱痕 提交于 2019-12-06 17:55:36
问题 If I declare a temporary auto deleted character buffer using std::auto_ptr<char> buffer(new char[n]); then the buffer is automatically deleted when the buffer goes out of scope. I would assume that the buffer is deleted using delete. However the buffer was created using new[], and so strictly speaking the buffer should be deleted using delete[]. What possibility is there that this mismatch might cause a memory leak? 回答1: The behaviour of calling delete on a pointer allocated with new[] is

Why does this code only print 42?

做~自己de王妃 提交于 2019-12-05 07:25:41
Could somebody please explain to me why does this code only print "42" instead of "created\n42"? #include <iostream> #include <string> #include <memory> using namespace std; class MyClass { public: MyClass() {cout<<"created"<<endl;}; int solution() {return 42;} virtual ~MyClass() {}; }; int main(int argc, char *argv[]) { auto_ptr<MyClass> ptr; cout<<ptr->solution()<<endl; return 0; } BTW I tried this code with different values in solution and I always get the "right" value, so it doesn't seem to be a random lucky value. Because it exhibits undefined behaviour - you dereference a null pointer.

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

孤街醉人 提交于 2019-12-05 02:06:17
In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeClass; // OK auto_ptr<NativeClass> mNativeClass; //Not allowed ! boost::shared_ptr<NativeClass> mNativeClass; //Not allowed ! }; Does anyone know of an equivalent of shared_ptr in the C++/CLI world? Edit: Thanks for your suggestion, "1800-Information". Following your

std::auto_ptr, delete[] and leaks

一世执手 提交于 2019-12-04 04:12:22
Why this code does not cause memory leaks? int iterCount = 1000; int sizeBig = 100000; for (int i = 0; i < iterCount; i++) { std::auto_ptr<char> buffer(new char[sizeBig]); } WinXP sp2, Compiler : BCB.05.03 Because you're (un)lucky. auto_ptr calls delete , not delete [] . This is undefined behavior. Try doing something like this and see if you get as lucky: struct Foo { char *bar; Foo(void) : bar(new char[100]) { } ~Foo(void) { delete [] bar; } } int iterCount = 1000; int sizeBig = 100000; for (int i = 0; i < iterCount; i++) { std::auto_ptr<Foo> buffer(new Foo[sizeBig]); } The idea here is that