![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Well the scope block was just an example to show how explicity how that auto_ptr manages memory. auto_ptr is actually perfect for use in a complex program, just not always with scope blocks like that. It allows you to create temporary heap objects and not have to explicitly delete them, even in the presence of exceptions. Let this example illustrate:
cpp Syntax (Toggle Plain Text)
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#12 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
I thought auto_ptr should never be used in STL containers due to ownership transfer issues when assigning one pointer to another.
|
|
|
|
|
|
#13 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Having never used auto_ptr in an actual program, I'm finding myself very unimpressed with it. The issue shown in the code I posted above, plus the thought of what might happen in a multi-threaded app, seem to make the use of auto_ptr extremely prone to errors if a developer isn't careful and thorough. I'm curious as to grumpy's opinion on it's use though, as he's probably seen it more than the rest of us (and he provides a more experienced perspective).
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#14 | |||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
auto_ptr is designed with a very precise purpose in mind. It meets that purpose beautifully. The biggest problem with it, however, is that programmers try to do more with it than it is designed for -- such as using it as a poor-mans garbage collector, or putting it into containers. Quote:
In practice, as long as the auto_ptr and the object it manages are only accessed by one thread, then auto_ptr can be used as intended. If it is shared by multiple threads, then it is necessary to protect access to the complete auto_ptr object (eg using critical sections or mutexes). That is equally true of any standard container (vector, list, etc) though. One reason why multi-threaded programs have to protect objects that are shared between threads is that objects can't generally protect themselves (i.e. behave correctly) if they are accessed from multiple threads. Quote:
Your little code example, which I recreate here, exhibits that; int main()
{
B* raw_ptr = new B(1);
{
std::auto_ptr<B> managed(raw_ptr);
managed->a->val = 24;
std::cout << managed->a->val << std::endl;
std::cout << raw_ptr->a->val << std::endl;
}
// auto_ptr now out of scope and object is deleted
std::cout << raw_ptr->a->val << std::endl;
return 0;
}There are a few other limitations of auto_ptr as well. One of those is that the assignment of an auto_ptr modifies both operands (i.e. "a = b;" where a and b are both auto_ptr objects, modifies both a and b). The reason for this is that assignment of auto_ptr effectively hands off control of the object being managed: a's object must be released, so that a can take over management of the object from b, and b will subsequently manage no object. This is not the sort of behaviour a standard container (vector, list, etc) expects of the objects it contains -- which is one reason why a vector of auto_ptr's is invalid. If you want to do things for which auto_ptr is not designed (eg place multiple managed pointers into a standard container) then auto_ptr is not the right tool. The Boost library provides a range of other "smart pointer" types that are better suited to such tasks. Each of those smart pointer types has different pros and cons, because they are designed for different use cases. The main practical problem with libraries like Boost is selecting the right smart pointer: each, like auto_ptr, is designed for a particular usage pattern and will not give desired results if used in other ways. Incidentally, Jimbo, the destructor for your type B needs to delete the contained A to avoid a memory leak. |
|||
|
|
|
|
|
#15 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
interesting...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|