1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
5 <META NAME="AUTHOR" CONTENT="pme@sources.redhat.com (Phil Edwards)">
6 <META NAME="KEYWORDS" CONTENT="HOWTO, libstdc++, GCC, g++, libg++, STL">
7 <META NAME="DESCRIPTION" CONTENT="HOWTO for the libstdc++ chapter 18.">
8 <META NAME="GENERATOR" CONTENT="vi and eight fingers">
9 <TITLE>libstdc++-v3 HOWTO: Chapter 18</TITLE>
10 <LINK REL=StyleSheet HREF="../lib3styles.css">
11 <!-- $Id: howto.html,v 1.7 2000/12/03 23:47:47 jsm28 Exp $ -->
15 <H1 CLASS="centered"><A NAME="top">Chapter 18: Library Support</A></H1>
17 <P>Chapter 18 deals with the functions called and objects created
18 automatically during the course of a program's existence.
20 <P>While we can't reproduce the contents of the Standard here (you need to
21 get your own copy from your nation's member body; see our homepage for
22 help), we can mention a couple of changes in what kind of support a C++
23 program gets from the Standard Library.
27 <!-- ####################################################### -->
31 <LI><A HREF="#1">Types</A>
32 <LI><A HREF="#2">Implementation properties</A>
33 <LI><A HREF="#3">Start and Termination</A>
34 <LI><A HREF="#4">Dynamic memory management</A>
39 <!-- ####################################################### -->
41 <H2><A NAME="1">Types</A></H2>
42 <P>All the types that you're used to in C are here in one form or
43 another. The only change that might affect people is the type of
44 NULL: while it is required to be a macro, the definition of that
45 macro is <EM>not</EM> allowed to be <TT>(void*)0</TT>, which is
48 <P>In g++, NULL is #define'd to be <TT>__null</TT>, a magic keyword
51 <P>The biggest problem of #defining NULL to be something like
52 "0L" is that the compiler will view that as a long integer
53 before it views it as a pointer, so overloading won't do what you
54 expect. (This is why g++ has a magic extension, so that NULL is
58 <A HREF="http://cseng.aw.com/bookdetail.qry?ISBN=0-201-92488-9&ptype=0"><EM>Effective C++</EM></A>,
59 Scott Meyers points out that the best way to solve this problem is to
60 not overload on pointer-vs-integer types to begin with. He also
61 offers a way to make your own magic NULL that will match pointers
62 before it matches integers:
64 const // this is a const object...
67 template<class T> // convertible to any type
68 operator T*() const // of null non-member
69 { return 0; } // pointer...
71 template<class C, class T> // or any type of null
72 operator T C::*() const // member pointer...
76 void operator&() const; // whose address can't be
77 // taken (see Item 27)...
79 } NULL; // and whose name is NULL
80 </PRE>(Cribbed from the published version of
81 <A HREF="http://www.awlonline.com/cseng/meyerscddemo/">the
82 Effective C++ CD</A>, reproduced here with permission.)
84 <P>If you aren't using g++ (why?), but you do have a compiler which
85 supports member function templates, then you can use this definition
86 of NULL (be sure to #undef any existing versions). It only helps if
87 you actually use NULL in function calls, though; if you make a call of
88 <TT>foo(0);</TT> instead of <TT>foo(NULL);</TT>, then you're back
91 <P><B>Added Note:</B> When we contacted Dr. Meyers to ask permission to
92 print this stuff, it prompted him to run this code through current
93 compilers to see what the state of the art is with respect to member
94 template functions. He posted
95 <A HREF="http://www.deja.com/threadmsg_md.xp?AN=644660779.1&CONTEXT=964036823.871301239">an
96 article to Usenet</A> after discovering that the code above is not
97 valid! Even though it has no data members, it still needs a
98 user-defined constructor (which means that the class needs a type name
99 after all). The ctor can have an empty body; it just needs to be
100 there. (Stupid requirement? We think so too, and this will probably
101 be changed in the language itself.)
103 <P>Return <A HREF="#top">to top of page</A> or
104 <A HREF="../faq/index.html">to the FAQ</A>.
108 <H2><A NAME="2">Implementation properties</A></H2>
110 <H3><CODE><limits></CODE></H3>
111 This header mainly defines traits classes to give access to various
112 implementation defined-aspects of the fundamental types. The
113 traits classes -- fourteen in total -- are all specilizations of the
114 template class <CODE>numeric_limits</CODE> defined as follows:
116 template<typename T> struct class {
117 static const bool is_specialized;
118 static T max() throw();
119 static T min() throw();
121 static const int digits;
122 static const int digits10;
123 static const bool is_signed;
124 static const bool is_integer;
125 static const bool is_exact;
126 static const int radix;
127 static T epsilon() throw();
128 static T round_error() throw();
130 static const int min_exponent;
131 static const int min_exponent10;
132 static const int max_exponent;
133 static const int max_exponent10;
135 static const bool has_infinity;
136 static const bool has_quiet_NaN;
137 static const bool has_signaling_NaN;
138 static const float_denorm_style has_denorm;
139 static const bool has_denorm_loss;
140 static T infinity() throw();
141 static T quiet_NaN() throw();
142 static T denorm_min() throw();
144 static const bool is_iec559;
145 static const bool is_bounded;
146 static const bool is_modulo;
148 static const bool traps;
149 static const bool tinyness_before;
150 static const float_round_style round_style;
153 <P>Return <A HREF="#top">to top of page</A> or
154 <A HREF="../faq/index.html">to the FAQ</A>.
158 <H2><A NAME="3">Start and Termination</A></H2>
159 <P>Not many changes here to <TT><cstdlib></TT> (the old stdlib.h).
160 You should note that the <TT>abort()</TT> function does not call
161 the destructors of automatic nor static objects, so if you're depending
162 on those to do cleanup, it isn't going to happen. (The functions
163 registered with <TT>atexit()</TT> don't get called either, so you
164 can forget about that possibility, too.)
166 <P>The good old <TT>exit()</TT> function can be a bit funky, too, until
167 you look closer. Basically, three points to remember are:
169 <LI>Static objects are destroyed in reverse order of their creation.
170 <LI>Functions registered with <TT>atexit()</TT> are called in
171 reverse order of registration, once per registration call.
172 (This isn't actually new.)
173 <LI>The previous two actions are "interleaved," that is,
176 extern "C or C++" void f1 (void);
177 extern "C or C++" void f2 (void);
183 </PRE>then at a call of <TT>exit()</TT>, f2 will be called, then
184 obj2 will be destroyed, then f1 will be called, and finally obj1
185 will be destroyed. If f1 or f2 allow an exception to propogate
186 out of them, Bad Things happen.
189 <P>Return <A HREF="#top">to top of page</A> or
190 <A HREF="../faq/index.html">to the FAQ</A>.
194 <H2><A NAME="4">Dynamic memory management</A></H2>
195 <P>There are six flavors each of <TT>new</TT> and <TT>delete</TT>, so
196 make certain that you're using the right ones! Here are quickie
197 descriptions of <TT>new</TT>:
199 <LI>single object form, throwing a <TT>bad_alloc</TT> on errors;
200 this is what most people are used to using
201 <LI>single object "nothrow" form, returning NULL on errors
202 <LI>array new, throwing <TT>bad_alloc</TT> on errors
203 <LI>array nothrow new, returning NULL on errors
204 <LI>placement new, which does nothing (like it's supposed to)
205 <LI>placement array new, which also does nothing
207 They are distinguished by the parameters that you pass to them, like
208 any other overloaded function. The six flavors of <TT>delete</TT>
209 are distinguished the same way, but none of them are allowed to throw
210 an exception under any circumstances anyhow. (They match up for
213 <P>Remember that it is perfectly okay to call <TT>delete</TT> on a
214 NULL pointer! Nothing happens, by definition. That is not the
215 same thing as deleting a pointer twice.
217 <P>By default, if one of the "throwing <TT>new</TT>s" can't
218 allocate the memory requested, it tosses an instance of a
219 <TT>bad_alloc</TT> exception (or, technically, some class derived
220 from it). You can change this by writing your own function (called
221 a new-handler) and then registering it with <TT>set_new_handler()</TT>:
223 typedef void (*PFV)(void);
226 static PFV old_handler;
228 void my_new_handler ()
231 popup_window ("Dude, you are running low on heap memory. You
232 should, like, close some windows, or something.
233 The next time you run out, we're gonna burn!");
234 set_new_handler (old_handler);
240 safety = new char[500000];
241 old_handler = set_new_handler (&my_new_handler);
246 <P><TT>bad_alloc</TT> is derived from the base <TT>exception</TT>
247 class defined in Chapter 19.
249 <P>Return <A HREF="#top">to top of page</A> or
250 <A HREF="../faq/index.html">to the FAQ</A>.
257 <!-- ####################################################### -->
260 <P CLASS="fineprint"><EM>
261 Comments and suggestions are welcome, and may be sent to
262 <A HREF="mailto:pme@sources.redhat.com">Phil Edwards</A> or
263 <A HREF="mailto:gdr@gcc.gnu.org">Gabriel Dos Reis</A>.
264 <BR> $Id: howto.html,v 1.7 2000/12/03 23:47:47 jsm28 Exp $