Some Rules about Using auto_ptr

发表于:2007-07-04来源:作者:点击数: 标签:
The auto_ptr template in C++ Standard Library provides us with a convenient way to allocating memory dynamically, however, we should follow some rules to use it, otherwise we may get in trouble. Here are some of these rules I extracted fro
>     The auto_ptr template in C++ Standard Library provides us with a
convenient way to allocating memory dynamically,
> however, we should follow some rules to use it, otherwise we may get
in trouble. Here are some of these rules I extracted
> from <<C++ Primer>> to share with the programmers in SS.

>  -----Original Message-----
> From: JunJun Chang XX (SH/RDC) 
> Sent:Wednesday, August 17, 2005 12:54 PM
> To:RDC SS (RDC)
> Subject:Some Rules about Using auto_ptr
>
>     The auto_ptr template in C++ Standard Library provides us with a
convenient way to allocating memory dynamically,
> however, we should follow some rules to use it, otherwise we may get
in trouble. Here are some of these rules I extracted
> from <<C++ Primer>> to share with the programmers in SS.
>
>     1. We can only use auto_ptr on single objects, i.e., dynamically
allocated arrays are not supported by auto_ptr, if we use it
>         on these arrays, the result is undefined.
>     2. We shouldn't assign an auto_ptr with a pointer witch is not
pointing to dynamically allocated space(i.e. which is not initialed
>         by the new operator), otherwise the behavior of the program
is undefined.
>     3. We should make sure that two different auto_ptrs are not
pointing to the same space. Otherwise, if one auto_ptr calls release(),  
>         the space that another auto_ptr is still pointing to may be
allocated to other objects, that may cause problem.
>
>     This is just my individual understanding of the auto_ptr, hope
that can help you.

原文转自:http://www.ltesting.net