2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
6 <chapter id="std.support" xreflabel="Support">
7 <?dbhtml filename="support.html"?>
22 <indexterm><primary>Support</primary></indexterm>
26 This part deals with the functions called and objects created
27 automatically during the course of a program's existence.
31 While we can't reproduce the contents of the Standard here (you
32 need to get your own copy from your nation's member body; see our
33 homepage for help), we can mention a couple of changes in what
34 kind of support a C++ program gets from the Standard Library.
37 <sect1 id="std.support.types" xreflabel="Types">
38 <?dbhtml filename="fundamental_types.html"?>
40 <sect2 id="std.support.types.fundamental" xreflabel="Fundamental Types">
41 <title>Fundamental Types</title>
43 C++ has the following builtin types:
91 These fundamental types are always available, without having to
92 include a header file. These types are exactly the same in
97 Specializing parts of the library on these types is prohibited:
102 <sect2 id="std.support.types.numeric_limits" xreflabel="Numeric Properties">
103 <title>Numeric Properties</title>
107 The header <filename class="headerfile">limits</filename> defines
108 traits classes to give access to various implementation
109 defined-aspects of the fundamental types. The traits classes --
110 fourteen in total -- are all specializations of the template class
111 <classname>numeric_limits</classname>, documented <ulink
112 url="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00593.html">here</ulink>
113 and defined as follows:
117 template<typename T>
120 static const bool is_specialized;
121 static T max() throw();
122 static T min() throw();
124 static const int digits;
125 static const int digits10;
126 static const bool is_signed;
127 static const bool is_integer;
128 static const bool is_exact;
129 static const int radix;
130 static T epsilon() throw();
131 static T round_error() throw();
133 static const int min_exponent;
134 static const int min_exponent10;
135 static const int max_exponent;
136 static const int max_exponent10;
138 static const bool has_infinity;
139 static const bool has_quiet_NaN;
140 static const bool has_signaling_NaN;
141 static const float_denorm_style has_denorm;
142 static const bool has_denorm_loss;
143 static T infinity() throw();
144 static T quiet_NaN() throw();
145 static T denorm_min() throw();
147 static const bool is_iec559;
148 static const bool is_bounded;
149 static const bool is_modulo;
151 static const bool traps;
152 static const bool tinyness_before;
153 static const float_round_style round_style;
158 <sect2 id="std.support.types.null" xreflabel="NULL">
161 The only change that might affect people is the type of
162 <constant>NULL</constant>: while it is required to be a macro,
163 the definition of that macro is <emphasis>not</emphasis> allowed
164 to be <constant>(void*)0</constant>, which is often used in C.
168 For <command>g++</command>, <constant>NULL</constant> is
169 <programlisting>#define</programlisting>'d to be
170 <constant>__null</constant>, a magic keyword extension of
171 <command>g++</command>.
175 The biggest problem of #defining <constant>NULL</constant> to be
176 something like <quote>0L</quote> is that the compiler will view
177 that as a long integer before it views it as a pointer, so
178 overloading won't do what you expect. (This is why
179 <command>g++</command> has a magic extension, so that
180 <constant>NULL</constant> is always a pointer.)
183 <para>In his book <ulink
184 url="http://www.awprofessional.com/titles/0-201-92488-9/"><emphasis>Effective
185 C++</emphasis></ulink>, Scott Meyers points out that the best way
186 to solve this problem is to not overload on pointer-vs-integer
187 types to begin with. He also offers a way to make your own magic
188 <constant>NULL</constant> that will match pointers before it
192 <ulink url="http://www.awprofessional.com/titles/0-201-31015-5/">the
193 Effective C++ CD example</ulink>
199 <sect1 id="std.support.memory" xreflabel="Dynamic Memory">
200 <?dbhtml filename="dynamic_memory.html"?>
201 <title>Dynamic Memory</title>
203 There are six flavors each of <function>new</function> and
204 <function>delete</function>, so make certain that you're using the right
205 ones. Here are quickie descriptions of <function>new</function>:
209 single object form, throwing a
210 <classname>bad_alloc</classname> on errors; this is what most
211 people are used to using
214 Single object "nothrow" form, returning NULL on errors
217 Array <function>new</function>, throwing
218 <classname>bad_alloc</classname> on errors
221 Array nothrow <function>new</function>, returning
222 <constant>NULL</constant> on errors
225 Placement <function>new</function>, which does nothing (like
229 Placement array <function>new</function>, which also does
234 They are distinguished by the parameters that you pass to them, like
235 any other overloaded function. The six flavors of <function>delete</function>
236 are distinguished the same way, but none of them are allowed to throw
237 an exception under any circumstances anyhow. (They match up for
241 Remember that it is perfectly okay to call <function>delete</function> on a
242 NULL pointer! Nothing happens, by definition. That is not the
243 same thing as deleting a pointer twice.
246 By default, if one of the <quote>throwing <function>new</function>s</quote> can't
247 allocate the memory requested, it tosses an instance of a
248 <classname>bad_alloc</classname> exception (or, technically, some class derived
249 from it). You can change this by writing your own function (called a
250 new-handler) and then registering it with <function>set_new_handler()</function>:
253 typedef void (*PFV)(void);
256 static PFV old_handler;
258 void my_new_handler ()
261 popup_window ("Dude, you are running low on heap memory. You
262 should, like, close some windows, or something.
263 The next time you run out, we're gonna burn!");
264 set_new_handler (old_handler);
270 safety = new char[500000];
271 old_handler = set_new_handler (&my_new_handler);
276 <classname>bad_alloc</classname> is derived from the base <classname>exception</classname>
277 class defined in Sect1 19.
281 <sect1 id="std.support.termination" xreflabel="Termination">
282 <?dbhtml filename="termination.html"?>
283 <title>Termination</title>
284 <sect2 id="support.termination.handlers" xreflabel="Termination Handlers">
285 <title>Termination Handlers</title>
287 Not many changes here to <filename
288 class="headerfile">cstdlib</filename>. You should note that the
289 <function>abort()</function> function does not call the
290 destructors of automatic nor static objects, so if you're
291 depending on those to do cleanup, it isn't going to happen.
292 (The functions registered with <function>atexit()</function>
293 don't get called either, so you can forget about that
297 The good old <function>exit()</function> function can be a bit
298 funky, too, until you look closer. Basically, three points to
304 Static objects are destroyed in reverse order of their creation.
309 Functions registered with <function>atexit()</function> are called in
310 reverse order of registration, once per registration call.
311 (This isn't actually new.)
316 The previous two actions are <quote>interleaved,</quote> that is,
317 given this pseudocode:
320 extern "C or C++" void f1 (void);
321 extern "C or C++" void f2 (void);
329 then at a call of <function>exit()</function>,
330 <varname>f2</varname> will be called, then
331 <varname>obj2</varname> will be destroyed, then
332 <varname>f1</varname> will be called, and finally
333 <varname>obj1</varname> will be destroyed. If
334 <varname>f1</varname> or <varname>f2</varname> allow an
335 exception to propagate out of them, Bad Things happen.
340 Note also that <function>atexit()</function> is only required to store 32
341 functions, and the compiler/library might already be using some of
342 those slots. If you think you may run out, we recommend using
343 the <function>xatexit</function>/<function>xexit</function> combination from <literal>libiberty</literal>, which has no such limit.
347 <sect2 id="support.termination.verbose" xreflabel="Verbose Terminate Handler">
348 <?dbhtml filename="verbose_termination.html"?>
349 <title>Verbose Terminate Handler</title>
351 If you are having difficulty with uncaught exceptions and want a
352 little bit of help debugging the causes of the core dumps, you can
353 make use of a GNU extension, the verbose terminate handler.
357 #include <exception>
361 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
364 throw <replaceable>anything</replaceable>;
369 The <function>__verbose_terminate_handler</function> function
370 obtains the name of the current exception, attempts to demangle
371 it, and prints it to stderr. If the exception is derived from
372 <classname>exception</classname> then the output from
373 <function>what()</function> will be included.
377 Any replacement termination function is required to kill the
378 program without returning; this one calls abort.
386 #include <exception>
387 #include <stdexcept>
389 struct argument_error : public std::runtime_error
391 argument_error(const std::string& s): std::runtime_error(s) { }
396 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
398 throw argument_error(<quote>argc is greater than 5!</quote>);
405 With the verbose terminate handler active, this gives:
411 terminate called after throwing a `int'
413 % ./a.out f f f f f f f f f f f
414 terminate called after throwing an instance of `argument_error'
415 what(): argc is greater than 5!
421 The 'Aborted' line comes from the call to
422 <function>abort()</function>, of course.
426 This is the default termination handler; nothing need be done to
427 use it. To go back to the previous <quote>silent death</quote>
428 method, simply include <filename>exception</filename> and
429 <filename>cstdlib</filename>, and call
433 std::set_terminate(std::abort);
437 After this, all calls to <function>terminate</function> will use
438 <function>abort</function> as the terminate handler.
442 Note: the verbose terminate handler will attempt to write to
443 stderr. If your application closes stderr or redirects it to an
444 inappropriate location,
445 <function>__verbose_terminate_handler</function> will behave in
446 an unspecified manner.