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

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc4/] [libstdc++-v3/] [doc/] [xml/] [manual/] [auto_ptr.xml] - Blame information for rev 519

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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