libstdc++
cstdlib
Go to the documentation of this file.
1 // -*- C++ -*- forwarding header.
2 
3 // Copyright (C) 1997-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file include/cstdlib
26  * This is a Standard C++ Library file. You should @c \#include this file
27  * in your programs, rather than any of the @a *.h implementation files.
28  *
29  * This is the C++ version of the Standard C Library header @c stdlib.h,
30  * and its contents are (mostly) the same as that header, but are all
31  * contained in the namespace @c std (except for names which are defined
32  * as macros in C).
33  */
34 
35 //
36 // ISO C++ 14882: 20.4.6 C library
37 //
38 
39 #ifndef _GLIBCXX_CSTDLIB
40 #define _GLIBCXX_CSTDLIB 1
41 
42 #ifdef _GLIBCXX_SYSHDR
43 #pragma GCC system_header
44 #endif
45 
46 #include <bits/c++config.h>
47 
48 #if !_GLIBCXX_HOSTED
49 // The C standard does not require a freestanding implementation to
50 // provide <stdlib.h>. However, the C++ standard does still require
51 // <cstdlib> -- but only the functionality mentioned in
52 // [lib.support.start.term].
53 
54 #define EXIT_SUCCESS 0
55 #define EXIT_FAILURE 1
56 #define NULL __null
57 
58 namespace std
59 {
60  extern "C" void abort(void) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;
61  extern "C" int atexit(void (*)(void)) _GLIBCXX_NOTHROW;
62  extern "C" void exit(int) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;
63 #if __cplusplus >= 201103L
64 # ifdef _GLIBCXX_HAVE_AT_QUICK_EXIT
65  extern "C" int at_quick_exit(void (*)(void)) _GLIBCXX_NOTHROW;
66 # endif
67 # ifdef _GLIBCXX_HAVE_QUICK_EXIT
68  extern "C" void quick_exit(int) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;
69 # endif
70 #if _GLIBCXX_USE_C99_STDLIB
71  extern "C" void _Exit(int) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;
72 #endif
73 #endif
74 } // namespace std
75 
76 #else
77 
78 // Need to ensure this finds the C library's <stdlib.h> not a libstdc++
79 // wrapper that might already be installed later in the include search path.
80 #pragma GCC diagnostic push
81 #pragma GCC diagnostic ignored "-Wpedantic" // include_next
82 #define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
83 #include_next <stdlib.h>
84 #undef _GLIBCXX_INCLUDE_NEXT_C_HEADERS
85 #pragma GCC diagnostic pop
86 
87 #include <bits/std_abs.h>
88 
89 // Get rid of those macros defined in <stdlib.h> in lieu of real functions.
90 #undef abort
91 #if __cplusplus >= 201703L && defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
92 # undef aligned_alloc
93 #endif
94 #undef atexit
95 #if __cplusplus >= 201103L
96 # ifdef _GLIBCXX_HAVE_AT_QUICK_EXIT
97 # undef at_quick_exit
98 # endif
99 #endif
100 #undef atof
101 #undef atoi
102 #undef atol
103 #undef bsearch
104 #undef calloc
105 #undef div
106 #undef exit
107 #undef free
108 #undef getenv
109 #undef labs
110 #undef ldiv
111 #undef malloc
112 #undef mblen
113 #undef mbstowcs
114 #undef mbtowc
115 #undef qsort
116 #if __cplusplus >= 201103L
117 # ifdef _GLIBCXX_HAVE_QUICK_EXIT
118 # undef quick_exit
119 # endif
120 #endif
121 #undef rand
122 #undef realloc
123 #undef srand
124 #undef strtod
125 #undef strtol
126 #undef strtoul
127 #undef system
128 #undef wcstombs
129 #undef wctomb
130 
131 extern "C++"
132 {
133 namespace std _GLIBCXX_VISIBILITY(default)
134 {
135 _GLIBCXX_BEGIN_NAMESPACE_VERSION
136 
137  using ::div_t;
138  using ::ldiv_t;
139 
140  using ::abort;
141 #if __cplusplus >= 201703L && defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
142  using ::aligned_alloc;
143 #endif
144  using ::atexit;
145 #if __cplusplus >= 201103L
146 # ifdef _GLIBCXX_HAVE_AT_QUICK_EXIT
147  using ::at_quick_exit;
148 # endif
149 #endif
150  using ::atof;
151  using ::atoi;
152  using ::atol;
153  using ::bsearch;
154  using ::calloc;
155  using ::div;
156  using ::exit;
157  using ::free;
158  using ::getenv;
159  using ::labs;
160  using ::ldiv;
161  using ::malloc;
162 #ifdef _GLIBCXX_HAVE_MBSTATE_T
163  using ::mblen;
164  using ::mbstowcs;
165  using ::mbtowc;
166 #endif // _GLIBCXX_HAVE_MBSTATE_T
167  using ::qsort;
168 #if __cplusplus >= 201103L
169 # ifdef _GLIBCXX_HAVE_QUICK_EXIT
170  using ::quick_exit;
171 # endif
172 #endif
173  using ::rand;
174  using ::realloc;
175  using ::srand;
176  using ::strtod;
177  using ::strtol;
178  using ::strtoul;
179  using ::system;
180 #ifdef _GLIBCXX_USE_WCHAR_T
181  using ::wcstombs;
182  using ::wctomb;
183 #endif // _GLIBCXX_USE_WCHAR_T
184 
185 #ifndef __CORRECT_ISO_CPP_STDLIB_H_PROTO
186  inline ldiv_t
187  div(long __i, long __j) _GLIBCXX_NOTHROW { return ldiv(__i, __j); }
188 #endif
189 
190 
191 _GLIBCXX_END_NAMESPACE_VERSION
192 } // namespace
193 
194 #if _GLIBCXX_USE_C99_STDLIB
195 
196 #undef _Exit
197 #undef llabs
198 #undef lldiv
199 #undef atoll
200 #undef strtoll
201 #undef strtoull
202 #undef strtof
203 #undef strtold
204 
205 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
206 {
207 _GLIBCXX_BEGIN_NAMESPACE_VERSION
208 
209 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
210  using ::lldiv_t;
211 #endif
212 #if _GLIBCXX_USE_C99_CHECK || _GLIBCXX_USE_C99_DYNAMIC
213  extern "C" void (_Exit)(int) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;
214 #endif
215 #if !_GLIBCXX_USE_C99_DYNAMIC
216  using ::_Exit;
217 #endif
218 
219 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
220 #pragma GCC diagnostic push
221 #pragma GCC diagnostic ignored "-Wlong-long"
222  using ::llabs;
223 
224  inline lldiv_t
225  div(long long __n, long long __d)
226  { lldiv_t __q; __q.quot = __n / __d; __q.rem = __n % __d; return __q; }
227 
228  using ::lldiv;
229 #pragma GCC diagnostic pop
230 #endif
231 
232 #if _GLIBCXX_USE_C99_LONG_LONG_CHECK || _GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
233  extern "C" long long int (atoll)(const char *) _GLIBCXX_NOTHROW;
234  extern "C" long long int
235  (strtoll)(const char * __restrict, char ** __restrict, int) _GLIBCXX_NOTHROW;
236  extern "C" unsigned long long int
237  (strtoull)(const char * __restrict, char ** __restrict, int) _GLIBCXX_NOTHROW;
238 #endif
239 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
240  using ::atoll;
241  using ::strtoll;
242  using ::strtoull;
243 #endif
244  using ::strtof;
245  using ::strtold;
246 
247 _GLIBCXX_END_NAMESPACE_VERSION
248 } // namespace __gnu_cxx
249 
250 namespace std
251 {
252 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
253  using ::__gnu_cxx::lldiv_t;
254 #endif
255  using ::__gnu_cxx::_Exit;
256 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
257  using ::__gnu_cxx::llabs;
258  using ::__gnu_cxx::div;
259  using ::__gnu_cxx::lldiv;
260 #endif
261  using ::__gnu_cxx::atoll;
262  using ::__gnu_cxx::strtof;
263  using ::__gnu_cxx::strtoll;
264  using ::__gnu_cxx::strtoull;
265  using ::__gnu_cxx::strtold;
266 } // namespace std
267 
268 #else // ! _GLIBCXX_USE_C99_STDLIB
269 
270 // We also check for strtof and strtold separately from _GLIBCXX_USE_C99_STDLIB
271 
272 #if _GLIBCXX_HAVE_STRTOF
273 #undef strtof
274 namespace std { using ::strtof; }
275 #endif
276 
277 #if _GLIBCXX_HAVE_STRTOLD
278 #undef strtold
279 namespace std { using ::strtold; }
280 #endif
281 
282 #endif // _GLIBCXX_USE_C99_STDLIB
283 
284 } // extern "C++"
285 
286 #endif // !_GLIBCXX_HOSTED
287 
288 #endif