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">
3 <html xmlns=
"http://www.w3.org/1999/xhtml"><head><meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" /><title>Chapter
5. Dynamic Memory
</title><meta name=
"generator" content=
"DocBook XSL Stylesheets V1.73.2" /><meta name=
"keywords" content=
" ISO C++ , library " /><link rel=
"start" href=
"../spine.html" title=
"The GNU C++ Library Documentation" /><link rel=
"up" href=
"support.html" title=
"Part II. Support" /><link rel=
"prev" href=
"bk01pt02ch04s03.html" title=
"NULL" /><link rel=
"next" href=
"bk01pt02ch06.html" title=
"Chapter 6. Termination" /></head><body><div class=
"navheader"><table width=
"100%" summary=
"Navigation header"><tr><th colspan=
"3" align=
"center">Chapter
5. Dynamic Memory
</th></tr><tr><td width=
"20%" align=
"left"><a accesskey=
"p" href=
"bk01pt02ch04s03.html">Prev
</a> </td><th width=
"60%" align=
"center">Part II. Support
</th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"bk01pt02ch06.html">Next
</a></td></tr></table><hr /></div><div class=
"chapter" lang=
"en" xml:
lang=
"en"><div class=
"titlepage"><div><div><h2 class=
"title"><a id=
"manual.support.memory"></a>Chapter
5. Dynamic Memory
</h2></div></div></div><p>
4 There are six flavors each of
<code class=
"function">new
</code> and
5 <code class=
"function">delete
</code>, so make certain that you're using the right
6 ones. Here are quickie descriptions of
<code class=
"function">new
</code>:
7 </p><div class=
"itemizedlist"><ul type=
"disc"><li><p>
8 single object form, throwing a
9 <code class=
"classname">bad_alloc
</code> on errors; this is what most
10 people are used to using
12 Single object
"nothrow" form, returning NULL on errors
14 Array
<code class=
"function">new
</code>, throwing
15 <code class=
"classname">bad_alloc
</code> on errors
17 Array nothrow
<code class=
"function">new
</code>, returning
18 <code class=
"constant">NULL
</code> on errors
20 Placement
<code class=
"function">new
</code>, which does nothing (like
23 Placement array
<code class=
"function">new
</code>, which also does
25 </p></li></ul></div><p>
26 They are distinguished by the parameters that you pass to them, like
27 any other overloaded function. The six flavors of
<code class=
"function">delete
</code>
28 are distinguished the same way, but none of them are allowed to throw
29 an exception under any circumstances anyhow. (They match up for
32 Remember that it is perfectly okay to call
<code class=
"function">delete
</code> on a
33 NULL pointer! Nothing happens, by definition. That is not the
34 same thing as deleting a pointer twice.
36 By default, if one of the “
<span class=
"quote">throwing
<code class=
"function">new
</code>s
</span>” can't
37 allocate the memory requested, it tosses an instance of a
38 <code class=
"classname">bad_alloc
</code> exception (or, technically, some class derived
39 from it). You can change this by writing your own function (called a
40 new-handler) and then registering it with
<code class=
"function">set_new_handler()
</code>:
41 </p><pre class=
"programlisting">
42 typedef void (*PFV)(void);
45 static PFV old_handler;
47 void my_new_handler ()
50 popup_window (
"Dude, you are running low on heap memory. You
51 should, like, close some windows, or something.
52 The next time you run out, we're gonna burn!");
53 set_new_handler (old_handler);
59 safety = new char[
500000];
60 old_handler = set_new_handler (
&my_new_handler);
64 <code class=
"classname">bad_alloc
</code> is derived from the base
<code class=
"classname">exception
</code>
65 class defined in Chapter
19.
66 </p></div><div class=
"navfooter"><hr /><table width=
"100%" summary=
"Navigation footer"><tr><td width=
"40%" align=
"left"><a accesskey=
"p" href=
"bk01pt02ch04s03.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=
"bk01pt02ch06.html">Next
</a></td></tr><tr><td width=
"40%" align=
"left" valign=
"top">NULL
</td><td width=
"20%" align=
"center"><a accesskey=
"h" href=
"../spine.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> Chapter
6. Termination
</td></tr></table></div></body></html>