2 <!DOCTYPE part PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
6 <part id="manual.support" xreflabel="Support">
7 <?dbhtml filename="support.html"?>
22 <indexterm><primary>Support</primary></indexterm>
28 This part deals with the functions called and objects created
29 automatically during the course of a program's existence.
33 While we can't reproduce the contents of the Standard here (you
34 need to get your own copy from your nation's member body; see our
35 homepage for help), we can mention a couple of changes in what
36 kind of support a C++ program gets from the Standard Library.
40 <chapter id="manual.support.types" xreflabel="Types">
41 <?dbhtml filename="fundamental_types.html"?>
43 <sect1 id="manual.support.types.fundamental" xreflabel="Fundamental Types">
44 <title>Fundamental Types</title>
46 C++ has the following builtin types:
94 These fundamental types are always available, without having to
95 include a header file. These types are exactly the same in
100 Specializing parts of the library on these types is prohibited:
105 <sect1 id="manual.support.types.numeric_limits" xreflabel="Numeric Properties">
106 <title>Numeric Properties</title>
110 The header <filename class="headerfile">limits</filename> defines
111 traits classes to give access to various implementation
112 defined-aspects of the fundamental types. The traits classes --
113 fourteen in total -- are all specializations of the template class
114 <classname>numeric_limits</classname>, documented <ulink
115 url="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/structstd_1_1numeric__limits.html">here</ulink>
116 and defined as follows:
120 template<typename T>
123 static const bool is_specialized;
124 static T max() throw();
125 static T min() throw();
127 static const int digits;
128 static const int digits10;
129 static const bool is_signed;
130 static const bool is_integer;
131 static const bool is_exact;
132 static const int radix;
133 static T epsilon() throw();
134 static T round_error() throw();
136 static const int min_exponent;
137 static const int min_exponent10;
138 static const int max_exponent;
139 static const int max_exponent10;
141 static const bool has_infinity;
142 static const bool has_quiet_NaN;
143 static const bool has_signaling_NaN;
144 static const float_denorm_style has_denorm;
145 static const bool has_denorm_loss;
146 static T infinity() throw();
147 static T quiet_NaN() throw();
148 static T denorm_min() throw();
150 static const bool is_iec559;
151 static const bool is_bounded;
152 static const bool is_modulo;
154 static const bool traps;
155 static const bool tinyness_before;
156 static const float_round_style round_style;
161 <sect1 id="manual.support.types.null" xreflabel="NULL">
164 The only change that might affect people is the type of
165 <constant>NULL</constant>: while it is required to be a macro,
166 the definition of that macro is <emphasis>not</emphasis> allowed
167 to be <constant>(void*)0</constant>, which is often used in C.
171 For <command>g++</command>, <constant>NULL</constant> is
172 <programlisting>#define</programlisting>'d to be
173 <constant>__null</constant>, a magic keyword extension of
174 <command>g++</command>.
178 The biggest problem of #defining <constant>NULL</constant> to be
179 something like <quote>0L</quote> is that the compiler will view
180 that as a long integer before it views it as a pointer, so
181 overloading won't do what you expect. (This is why
182 <command>g++</command> has a magic extension, so that
183 <constant>NULL</constant> is always a pointer.)
186 <para>In his book <ulink
187 url="http://www.awprofessional.com/titles/0-201-92488-9/"><emphasis>Effective
188 C++</emphasis></ulink>, Scott Meyers points out that the best way
189 to solve this problem is to not overload on pointer-vs-integer
190 types to begin with. He also offers a way to make your own magic
191 <constant>NULL</constant> that will match pointers before it
195 <ulink url="http://www.awprofessional.com/titles/0-201-31015-5/">the
196 Effective C++ CD example</ulink>
202 <chapter id="manual.support.memory" xreflabel="Dynamic Memory">
203 <?dbhtml filename="dynamic_memory.html"?>
204 <title>Dynamic Memory</title>
206 There are six flavors each of <function>new</function> and
207 <function>delete</function>, so make certain that you're using the right
208 ones. Here are quickie descriptions of <function>new</function>:
212 single object form, throwing a
213 <classname>bad_alloc</classname> on errors; this is what most
214 people are used to using
217 Single object "nothrow" form, returning NULL on errors
220 Array <function>new</function>, throwing
221 <classname>bad_alloc</classname> on errors
224 Array nothrow <function>new</function>, returning
225 <constant>NULL</constant> on errors
228 Placement <function>new</function>, which does nothing (like
232 Placement array <function>new</function>, which also does
237 They are distinguished by the parameters that you pass to them, like
238 any other overloaded function. The six flavors of <function>delete</function>
239 are distinguished the same way, but none of them are allowed to throw
240 an exception under any circumstances anyhow. (They match up for
244 Remember that it is perfectly okay to call <function>delete</function> on a
245 NULL pointer! Nothing happens, by definition. That is not the
246 same thing as deleting a pointer twice.
249 By default, if one of the <quote>throwing <function>new</function>s</quote> can't
250 allocate the memory requested, it tosses an instance of a
251 <classname>bad_alloc</classname> exception (or, technically, some class derived
252 from it). You can change this by writing your own function (called a
253 new-handler) and then registering it with <function>set_new_handler()</function>:
256 typedef void (*PFV)(void);
259 static PFV old_handler;
261 void my_new_handler ()
264 popup_window ("Dude, you are running low on heap memory. You
265 should, like, close some windows, or something.
266 The next time you run out, we're gonna burn!");
267 set_new_handler (old_handler);
273 safety = new char[500000];
274 old_handler = set_new_handler (&my_new_handler);
279 <classname>bad_alloc</classname> is derived from the base <classname>exception</classname>
280 class defined in Chapter 19.
284 <chapter id="manual.support.termination" xreflabel="Termination">
285 <?dbhtml filename="termination.html"?>
286 <title>Termination</title>
287 <sect1 id="support.termination.handlers" xreflabel="Termination Handlers">
288 <title>Termination Handlers</title>
290 Not many changes here to <filename
291 class="headerfile">cstdlib</filename>. You should note that the
292 <function>abort()</function> function does not call the
293 destructors of automatic nor static objects, so if you're
294 depending on those to do cleanup, it isn't going to happen.
295 (The functions registered with <function>atexit()</function>
296 don't get called either, so you can forget about that
300 The good old <function>exit()</function> function can be a bit
301 funky, too, until you look closer. Basically, three points to
307 Static objects are destroyed in reverse order of their creation.
312 Functions registered with <function>atexit()</function> are called in
313 reverse order of registration, once per registration call.
314 (This isn't actually new.)
319 The previous two actions are <quote>interleaved,</quote> that is,
320 given this pseudocode:
323 extern "C or C++" void f1 (void);
324 extern "C or C++" void f2 (void);
332 then at a call of <function>exit()</function>,
333 <varname>f2</varname> will be called, then
334 <varname>obj2</varname> will be destroyed, then
335 <varname>f1</varname> will be called, and finally
336 <varname>obj1</varname> will be destroyed. If
337 <varname>f1</varname> or <varname>f2</varname> allow an
338 exception to propagate out of them, Bad Things happen.
343 Note also that <function>atexit()</function> is only required to store 32
344 functions, and the compiler/library might already be using some of
345 those slots. If you think you may run out, we recommend using
346 the <function>xatexit</function>/<function>xexit</function> combination from <literal>libiberty</literal>, which has no such limit.
350 <sect1 id="support.termination.verbose" xreflabel="Verbose Terminate Handler">
351 <?dbhtml filename="verbose_termination.html"?>
352 <title>Verbose Terminate Handler</title>
354 If you are having difficulty with uncaught exceptions and want a
355 little bit of help debugging the causes of the core dumps, you can
356 make use of a GNU extension, the verbose terminate handler.
360 #include <exception>
364 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
367 throw <replaceable>anything</replaceable>;
372 The <function>__verbose_terminate_handler</function> function
373 obtains the name of the current exception, attempts to demangle
374 it, and prints it to stderr. If the exception is derived from
375 <classname>exception</classname> then the output from
376 <function>what()</function> will be included.
380 Any replacement termination function is required to kill the
381 program without returning; this one calls abort.
389 #include <exception>
390 #include <stdexcept>
392 struct argument_error : public std::runtime_error
394 argument_error(const std::string& s): std::runtime_error(s) { }
399 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
401 throw argument_error(<quote>argc is greater than 5!</quote>);
408 With the verbose terminate handler active, this gives:
414 terminate called after throwing a `int'
416 % ./a.out f f f f f f f f f f f
417 terminate called after throwing an instance of `argument_error'
418 what(): argc is greater than 5!
424 The 'Aborted' line comes from the call to
425 <function>abort()</function>, of course.
429 This is the default termination handler; nothing need be done to
430 use it. To go back to the previous <quote>silent death</quote>
431 method, simply include <filename>exception</filename> and
432 <filename>cstdlib</filename>, and call
436 std::set_terminate(std::abort);
440 After this, all calls to <function>terminate</function> will use
441 <function>abort</function> as the terminate handler.
445 Note: the verbose terminate handler will attempt to write to
446 stderr. If your application closes stderr or redirects it to an
447 inappropriate location,
448 <function>__verbose_terminate_handler</function> will behave in
449 an unspecified manner.