1 <html><head><meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"><title>Dynamic Memory
</title><meta name=
"generator" content=
"DocBook XSL-NS Stylesheets V1.76.1"><meta name=
"keywords" content=
"
5 "><meta name=
"keywords" content=
"
11 "><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 bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF"><div class=
"navheader"><table width=
"100%" summary=
"Navigation header"><tr><th colspan=
"3" align=
"center">Dynamic Memory
</th></tr><tr><td width=
"20%" align=
"left"><a accesskey=
"p" href=
"support.html">Prev
</a> </td><th width=
"60%" align=
"center">Chapter
4.
14 </th><td width=
"20%" 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" style=
"clear: both"><a name=
"std.support.memory"></a>Dynamic Memory
</h2></div></div></div><p>
15 There are six flavors each of
<code class=
"function">new
</code> and
16 <code class=
"function">delete
</code>, so make certain that you're using the right
17 ones. Here are quickie descriptions of
<code class=
"function">new
</code>:
18 </p><div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc"><li class=
"listitem"><p>
19 single object form, throwing a
20 <code class=
"classname">bad_alloc
</code> on errors; this is what most
21 people are used to using
22 </p></li><li class=
"listitem"><p>
23 Single object
"nothrow" form, returning NULL on errors
24 </p></li><li class=
"listitem"><p>
25 Array
<code class=
"function">new
</code>, throwing
26 <code class=
"classname">bad_alloc
</code> on errors
27 </p></li><li class=
"listitem"><p>
28 Array nothrow
<code class=
"function">new
</code>, returning
29 <code class=
"constant">NULL
</code> on errors
30 </p></li><li class=
"listitem"><p>
31 Placement
<code class=
"function">new
</code>, which does nothing (like
33 </p></li><li class=
"listitem"><p>
34 Placement array
<code class=
"function">new
</code>, which also does
36 </p></li></ul></div><p>
37 They are distinguished by the parameters that you pass to them, like
38 any other overloaded function. The six flavors of
<code class=
"function">delete
</code>
39 are distinguished the same way, but none of them are allowed to throw
40 an exception under any circumstances anyhow. (They match up for
43 Remember that it is perfectly okay to call
<code class=
"function">delete
</code> on a
44 NULL pointer! Nothing happens, by definition. That is not the
45 same thing as deleting a pointer twice.
47 By default, if one of the
<span class=
"quote">“
<span class=
"quote">throwing
<code class=
"function">new
</code>s
</span>”
</span> can't
48 allocate the memory requested, it tosses an instance of a
49 <code class=
"classname">bad_alloc
</code> exception (or, technically, some class derived
50 from it). You can change this by writing your own function (called a
51 new-handler) and then registering it with
<code class=
"function">set_new_handler()
</code>:
52 </p><pre class=
"programlisting">
53 typedef void (*PFV)(void);
56 static PFV old_handler;
58 void my_new_handler ()
61 popup_window (
"Dude, you are running low on heap memory. You
62 should, like, close some windows, or something.
63 The next time you run out, we're gonna burn!");
64 set_new_handler (old_handler);
70 safety = new char[
500000];
71 old_handler = set_new_handler (
&my_new_handler);
75 <code class=
"classname">bad_alloc
</code> is derived from the base
<code class=
"classname">exception
</code>
76 class defined in Sect1
19.
77 </p></div><div class=
"navfooter"><hr><table width=
"100%" summary=
"Navigation footer"><tr><td width=
"40%" align=
"left"><a accesskey=
"p" href=
"support.html">Prev
</a> </td><td width=
"20%" align=
"center"><a accesskey=
"u" href=
"support.html">Up
</a></td><td width=
"40%" align=
"right"> <a accesskey=
"n" href=
"termination.html">Next
</a></td></tr><tr><td width=
"40%" align=
"left" valign=
"top">Chapter
4.
80 </td><td width=
"20%" align=
"center"><a accesskey=
"h" href=
"../index.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> Termination
</td></tr></table></div></body></html>