1 <?xml version=
"1.0" encoding=
"UTF-8" standalone=
"no"?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns=
"http://www.w3.org/1999/xhtml"><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.78.1" /><meta name=
"keywords" content=
"ISO C++, library" /><meta name=
"keywords" content=
"ISO C++, runtime, library" /><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 width=
"20%" align=
"left"><a accesskey=
"p" href=
"support.html">Prev
</a> </td><th width=
"60%" align=
"center">Chapter
4.
5 </th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"termination.html">Next
</a></td></tr></table><hr /></div><div class=
"section"><div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both"><a id=
"std.support.memory"></a>Dynamic Memory
</h2></div></div></div><p>
6 There are six flavors each of
<code class=
"function">new
</code> and
7 <code class=
"function">delete
</code>, so make certain that you're using the right
8 ones. Here are quickie descriptions of
<code class=
"function">new
</code>:
9 </p><div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; "><li class=
"listitem"><p>
10 single object form, throwing a
11 <code class=
"classname">bad_alloc
</code> on errors; this is what most
12 people are used to using
13 </p></li><li class=
"listitem"><p>
14 Single object
"nothrow" form, returning NULL on errors
15 </p></li><li class=
"listitem"><p>
16 Array
<code class=
"function">new
</code>, throwing
17 <code class=
"classname">bad_alloc
</code> on errors
18 </p></li><li class=
"listitem"><p>
19 Array nothrow
<code class=
"function">new
</code>, returning
20 <code class=
"constant">NULL
</code> on errors
21 </p></li><li class=
"listitem"><p>
22 Placement
<code class=
"function">new
</code>, which does nothing (like
24 </p></li><li class=
"listitem"><p>
25 Placement array
<code class=
"function">new
</code>, which also does
27 </p></li></ul></div><p>
28 They are distinguished by the parameters that you pass to them, like
29 any other overloaded function. The six flavors of
<code class=
"function">delete
</code>
30 are distinguished the same way, but none of them are allowed to throw
31 an exception under any circumstances anyhow. (They match up for
34 Remember that it is perfectly okay to call
<code class=
"function">delete
</code> on a
35 NULL pointer! Nothing happens, by definition. That is not the
36 same thing as deleting a pointer twice.
38 By default, if one of the
<span class=
"quote">“
<span class=
"quote">throwing
<code class=
"function">new
</code>s
</span>”
</span> can't
39 allocate the memory requested, it tosses an instance of a
40 <code class=
"classname">bad_alloc
</code> exception (or, technically, some class derived
41 from it). You can change this by writing your own function (called a
42 new-handler) and then registering it with
<code class=
"function">set_new_handler()
</code>:
43 </p><pre class=
"programlisting">
44 typedef void (*PFV)(void);
47 static PFV old_handler;
49 void my_new_handler ()
52 popup_window (
"Dude, you are running low on heap memory. You"
53 " should, like, close some windows, or something."
54 " The next time you run out, we're gonna burn!");
55 set_new_handler (old_handler);
61 safety = new char[
500000];
62 old_handler = set_new_handler (
&my_new_handler);
66 <code class=
"classname">bad_alloc
</code> is derived from the base
<code class=
"classname">exception
</code>
67 class defined in Sect1
19.
68 </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.
71 </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>