what's the point of auto_ptr & unique_ptr?
-
I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '
new
' operator, right? Here is a little code snippet:
class Base
{
public:
Base()
{
cout << "Base::()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}virtual void iPhone() { cout << "I'm iPhone5s" << endl; }
};
class Derive : public Base
{
public:Derive() { cout << "Derive()" << endl; } virtual void iPhone() { cout << "I'm iPhone 6" << endl; } virtual ~Derive() { cout << "~Derive()" << endl; }
};
void main()
{
{auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so? ptr->iPhone(); Derive d;//isn't it better? Base\* base = &d; base->iPhone();
}
} -
I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '
new
' operator, right? Here is a little code snippet:
class Base
{
public:
Base()
{
cout << "Base::()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}virtual void iPhone() { cout << "I'm iPhone5s" << endl; }
};
class Derive : public Base
{
public:Derive() { cout << "Derive()" << endl; } virtual void iPhone() { cout << "I'm iPhone 6" << endl; } virtual ~Derive() { cout << "~Derive()" << endl; }
};
void main()
{
{auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so? ptr->iPhone(); Derive d;//isn't it better? Base\* base = &d; base->iPhone();
}
}Note:
std::auto_ptr
has been (mostly) replaced bystd::unique_ptr
. The use ofstd::unique_ptr
andstd::shared_ptr
offer better memory management (garbage control, ... ) than naked pointer (google for the difference between unique and shared).{
Derive* p = new Derive;
// will leak here
}{
std::unique_ptr p(new Derive);
// p will be deleted here, no leak.
}In all (most) cases it would be better to not use pointers at all; but when it happens, use std::unique_ptr or stdshared_ptr depending on your need. So in your example, it could simple be :
{
Derive d;
d.iPhone(); // no need to access the Base class.
}In simple examples using one or the other will probably not make a difference, but in larger systems, it will.
I'd rather be phishing!
-
I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '
new
' operator, right? Here is a little code snippet:
class Base
{
public:
Base()
{
cout << "Base::()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}virtual void iPhone() { cout << "I'm iPhone5s" << endl; }
};
class Derive : public Base
{
public:Derive() { cout << "Derive()" << endl; } virtual void iPhone() { cout << "I'm iPhone 6" << endl; } virtual ~Derive() { cout << "~Derive()" << endl; }
};
void main()
{
{auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so? ptr->iPhone(); Derive d;//isn't it better? Base\* base = &d; base->iPhone();
}
}It's essentially meant for the cases where you do need the heap... when you're already using new/delete. It eliminates the need to call delete explicitly and prevents leaks by taking care of the deallocation automatically. That's pretty much the crux of it (I personally don't use it at all).
-
It's essentially meant for the cases where you do need the heap... when you're already using new/delete. It eliminates the need to call delete explicitly and prevents leaks by taking care of the deallocation automatically. That's pretty much the crux of it (I personally don't use it at all).
I never found it especially useful. By the time it arrived I had disciplined myself when I created code that I didn't see failures to deallocate anymore. I did find that attempting to refactor code (others) that did have problems was at best a futile exorcize but maybe I never understood it in detail enough.
-
I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '
new
' operator, right? Here is a little code snippet:
class Base
{
public:
Base()
{
cout << "Base::()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}virtual void iPhone() { cout << "I'm iPhone5s" << endl; }
};
class Derive : public Base
{
public:Derive() { cout << "Derive()" << endl; } virtual void iPhone() { cout << "I'm iPhone 6" << endl; } virtual ~Derive() { cout << "~Derive()" << endl; }
};
void main()
{
{auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so? ptr->iPhone(); Derive d;//isn't it better? Base\* base = &d; base->iPhone();
}
} -
I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '
new
' operator, right? Here is a little code snippet:
class Base
{
public:
Base()
{
cout << "Base::()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}virtual void iPhone() { cout << "I'm iPhone5s" << endl; }
};
class Derive : public Base
{
public:Derive() { cout << "Derive()" << endl; } virtual void iPhone() { cout << "I'm iPhone 6" << endl; } virtual ~Derive() { cout << "~Derive()" << endl; }
};
void main()
{
{auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so? ptr->iPhone(); Derive d;//isn't it better? Base\* base = &d; base->iPhone();
}
}In general it may indeed be better to create it on the stack. The reason you'd use
new
is when you want more control over it's lifetime. For example you can't use the stack if it need to stay "alive" after the function returns (more generally, when it goes out of scope).Steve