faq.xml: Replace references to C++0x with C++11.
[gcc.git] / libstdc++-v3 / doc / html / manual / dynamic_memory.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Dynamic Memory</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"/><meta name="keywords" content="&#10; ISO C++&#10; , &#10; library&#10; "/><meta name="keywords" content="&#10; ISO C++&#10; , &#10; runtime&#10; , &#10; library&#10; "/><link rel="home" href="../index.html" title="The GNU C++ Library"/><link rel="up" href="support.html" title="Chapter 4.  Support"/><link rel="prev" href="support.html" title="Chapter 4.  Support"/><link rel="next" href="termination.html" title="Termination"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Dynamic Memory</th></tr><tr><td align="left"><a accesskey="p" href="support.html">Prev</a> </td><th width="60%" align="center">Chapter 4
4 Support
5
6 </th><td align="right"> <a accesskey="n" href="termination.html">Next</a></td></tr></table><hr/></div><div class="section" title="Dynamic Memory"><div class="titlepage"><div><div><h2 class="title"><a id="std.support.memory"/>Dynamic Memory</h2></div></div></div><p>
7 There are six flavors each of <code class="function">new</code> and
8 <code class="function">delete</code>, so make certain that you're using the right
9 ones. Here are quickie descriptions of <code class="function">new</code>:
10 </p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>
11 single object form, throwing a
12 <code class="classname">bad_alloc</code> on errors; this is what most
13 people are used to using
14 </p></li><li class="listitem"><p>
15 Single object "nothrow" form, returning NULL on errors
16 </p></li><li class="listitem"><p>
17 Array <code class="function">new</code>, throwing
18 <code class="classname">bad_alloc</code> on errors
19 </p></li><li class="listitem"><p>
20 Array nothrow <code class="function">new</code>, returning
21 <code class="constant">NULL</code> on errors
22 </p></li><li class="listitem"><p>
23 Placement <code class="function">new</code>, which does nothing (like
24 it's supposed to)
25 </p></li><li class="listitem"><p>
26 Placement array <code class="function">new</code>, which also does
27 nothing
28 </p></li></ul></div><p>
29 They are distinguished by the parameters that you pass to them, like
30 any other overloaded function. The six flavors of <code class="function">delete</code>
31 are distinguished the same way, but none of them are allowed to throw
32 an exception under any circumstances anyhow. (They match up for
33 completeness' sake.)
34 </p><p>
35 Remember that it is perfectly okay to call <code class="function">delete</code> on a
36 NULL pointer! Nothing happens, by definition. That is not the
37 same thing as deleting a pointer twice.
38 </p><p>
39 By default, if one of the <span class="quote"><span class="quote">throwing <code class="function">new</code>s</span></span> can't
40 allocate the memory requested, it tosses an instance of a
41 <code class="classname">bad_alloc</code> exception (or, technically, some class derived
42 from it). You can change this by writing your own function (called a
43 new-handler) and then registering it with <code class="function">set_new_handler()</code>:
44 </p><pre class="programlisting">
45 typedef void (*PFV)(void);
46
47 static char* safety;
48 static PFV old_handler;
49
50 void my_new_handler ()
51 {
52 delete[] safety;
53 popup_window ("Dude, you are running low on heap memory. You
54 should, like, close some windows, or something.
55 The next time you run out, we're gonna burn!");
56 set_new_handler (old_handler);
57 return;
58 }
59
60 int main ()
61 {
62 safety = new char[500000];
63 old_handler = set_new_handler (&amp;my_new_handler);
64 ...
65 }
66 </pre><p>
67 <code class="classname">bad_alloc</code> is derived from the base <code class="classname">exception</code>
68 class defined in Sect1 19.
69 </p></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td align="left"><a accesskey="p" href="support.html">Prev</a> </td><td align="center"><a accesskey="u" href="support.html">Up</a></td><td align="right"> <a accesskey="n" href="termination.html">Next</a></td></tr><tr><td align="left" valign="top">Chapter 4
70 Support
71
72  </td><td align="center"><a accesskey="h" href="../index.html">Home</a></td><td align="right" valign="top"> Termination</td></tr></table></div></body></html>