OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [doc/] [xml/] [manual/] [auto_ptr.xml] - Blame information for rev 745

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
2
         xml:id="std.util.memory.auto_ptr" xreflabel="auto_ptr">
3
4
 
5
auto_ptr
6
  
7
    
8
      ISO C++
9
    
10
    
11
      auto_ptr
12
    
13
  
14
15
 
16
 
17
 
18
Limitations
19
 
20
 
21
   Explaining all of the fun and delicious things that can
22
   happen with misuse of the auto_ptr class
23
   template (called AP here) would take some
24
   time. Suffice it to say that the use of AP
25
   safely in the presence of copying has some subtleties.
26
   
27
   
28
     The AP class is a really
29
      nifty idea for a smart pointer, but it is one of the dumbest of
30
      all the smart pointers -- and that's fine.
31
   
32
   
33
     AP is not meant to be a supersmart solution to all resource
34
      leaks everywhere.  Neither is it meant to be an effective form
35
      of garbage collection (although it can help, a little bit).
36
      And it can notbe used for arrays!
37
   
38
   
39
     AP is meant to prevent nasty leaks in the
40
     presence of exceptions.  That's all.  This
41
     code is AP-friendly:
42
   
43
   
44
    // Not a recommend naming scheme, but good for web-based FAQs.
45
    typedef std::auto_ptr<MyClass>  APMC;
46
 
47
    extern function_taking_MyClass_pointer (MyClass*);
48
    extern some_throwable_function ();
49
 
50
    void func (int data)
51
    {
52
        APMC  ap (new MyClass(data));
53
 
54
        some_throwable_function();   // this will throw an exception
55
 
56
        function_taking_MyClass_pointer (ap.get());
57
    }
58
   
59
   When an exception gets thrown, the instance of MyClass that's
60
      been created on the heap will be delete'd as the stack is
61
      unwound past func().
62
   
63
   Changing that code as follows is not AP-friendly:
64
   
65
   
66
        APMC  ap (new MyClass[22]);
67
   
68
   You will get the same problems as you would without the use
69
      of AP:
70
   
71
   
72
        char*  array = new char[10];       // array new...
73
        ...
74
        delete array;                      // ...but single-object delete
75
   
76
   
77
     AP cannot tell whether the pointer you've passed at creation points
78
      to one or many things.  If it points to many things, you are about
79
      to die.  AP is trivial to write, however, so you could write your
80
      own auto_array_ptr for that situation (in fact, this has
81
      been done many times; check the mailing lists, Usenet, Boost, etc).
82
   
83
84
 
85
Use in Containers
86
 
87
 
88
  
89
  
90
  All of the containers
91
      described in the standard library require their contained types
92
      to have, among other things, a copy constructor like this:
93
  
94
   
95
    struct My_Type
96
    {
97
        My_Type (My_Type const&);
98
    };
99
   
100
   
101
     Note the const keyword; the object being copied shouldn't change.
102
     The template class auto_ptr (called AP here) does not
103
     meet this requirement.  Creating a new AP by copying an existing
104
     one transfers ownership of the pointed-to object, which means that
105
     the AP being copied must change, which in turn means that the
106
     copy ctors of AP do not take const objects.
107
   
108
   
109
     The resulting rule is simple: Never ever use a
110
     container of auto_ptr objects. The standard says that
111
     undefined behavior is the result, but it is
112
     guaranteed to be messy.
113
   
114
   
115
     To prevent you from doing this to yourself, the
116
      concept checks built
117
      in to this implementation will issue an error if you try to
118
      compile code like this:
119
   
120
   
121
    #include <vector>
122
    #include <memory>
123
 
124
    void f()
125
    {
126
        std::vector< std::auto_ptr<int> >   vec_ap_int;
127
    }
128
   
129
   
130
Should you try this with the checks enabled, you will see an error.
131
   
132
133
 
134

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.