Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/header/cstdlib"

From cppreference.com
< cpp‎ | header
m
(Applied P2338R4 (Freestanding Library: Character primitives and the C library).)
 
(2 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
This header provides miscellaneous utilities. Symbols defined here are used by several library components.
 
This header provides miscellaneous utilities. Symbols defined here are used by several library components.
 
{{rev begin}}
 
{{rev| This header is a partial [[cpp/freestanding|freestanding]] header.}}
 
{{rev end}}
 
  
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc h1 | Types}}
+
{{dsc h1|Types}}
{{dsc inc | cpp/numeric/math/dsc div_t}}
+
{{dsc inc|cpp/numeric/math/dsc div_t}}
{{dsc inc | cpp/numeric/math/dsc ldiv_t}}
+
{{dsc inc|cpp/numeric/math/dsc ldiv_t}}
{{dsc inc | cpp/numeric/math/dsc lldiv_t}}
+
{{dsc inc|cpp/numeric/math/dsc lldiv_t}}
{{dsc inc | cpp/types/dsc size_t}}
+
{{dsc inc|cpp/types/dsc size_t}}
  
{{dsc h1 | Macro constants}}
+
{{dsc h1|Macro constants}}
{{dsc inc | cpp/utility/program/dsc EXIT_status}}
+
{{dsc inc|cpp/utility/program/dsc EXIT_status}}
{{dsc inc | cpp/numeric/random/dsc MB_CUR_MAX}}
+
{{dsc inc|cpp/numeric/random/dsc MB_CUR_MAX}}
{{dsc inc | cpp/types/dsc NULL}}
+
{{dsc inc|cpp/types/dsc NULL}}
{{dsc inc | cpp/numeric/random/dsc RAND_MAX}}
+
{{dsc inc|cpp/numeric/random/dsc RAND_MAX}}
{{dsc h1 | Functions}}
+
{{dsc h1|Functions}}
{{dsc h2 | Process control}}
+
{{dsc h2|Process control}}
{{dsc inc | cpp/utility/program/dsc abort}}
+
{{dsc inc|cpp/utility/program/dsc abort}}
{{dsc inc | cpp/utility/program/dsc exit}}
+
{{dsc inc|cpp/utility/program/dsc exit}}
{{dsc inc | cpp/utility/program/dsc quick_exit}}
+
{{dsc inc|cpp/utility/program/dsc quick_exit}}
{{dsc inc | cpp/utility/program/dsc _Exit}}
+
{{dsc inc|cpp/utility/program/dsc _Exit}}
{{dsc inc | cpp/utility/program/dsc atexit}}
+
{{dsc inc|cpp/utility/program/dsc atexit}}
{{dsc inc | cpp/utility/program/dsc at_quick_exit}}
+
{{dsc inc|cpp/utility/program/dsc at_quick_exit}}
{{dsc inc | cpp/utility/program/dsc system}}
+
{{dsc inc|cpp/utility/program/dsc system}}
{{dsc inc | cpp/utility/program/dsc getenv}}
+
{{dsc inc|cpp/utility/program/dsc getenv}}
  
{{dsc h2 | Memory management}}
+
{{dsc h2|Memory management}}
{{dsc inc | cpp/memory/c/dsc malloc}}
+
{{dsc inc|cpp/memory/c/dsc malloc}}
{{dsc inc | cpp/memory/c/dsc aligned_alloc}}
+
{{dsc inc|cpp/memory/c/dsc aligned_alloc}}
{{dsc inc | cpp/memory/c/dsc calloc}}
+
{{dsc inc|cpp/memory/c/dsc calloc}}
{{dsc inc | cpp/memory/c/dsc realloc}}
+
{{dsc inc|cpp/memory/c/dsc realloc}}
{{dsc inc | cpp/memory/c/dsc free}}
+
{{dsc inc|cpp/memory/c/dsc free}}
  
{{dsc h2 | Numeric string conversion}}
+
{{dsc h2|Numeric string conversion}}
{{dsc inc | cpp/string/byte/dsc atof}}
+
{{dsc inc|cpp/string/byte/dsc atof}}
{{dsc inc | cpp/string/byte/dsc atoi}}
+
{{dsc inc|cpp/string/byte/dsc atoi}}
{{dsc inc | cpp/string/byte/dsc strtol}}
+
{{dsc inc|cpp/string/byte/dsc strtol}}
{{dsc inc | cpp/string/byte/dsc strtoul}}
+
{{dsc inc|cpp/string/byte/dsc strtoul}}
{{dsc inc | cpp/string/byte/dsc strtof}}
+
{{dsc inc|cpp/string/byte/dsc strtof}}
  
{{dsc h2 | Wide string manipulation}}
+
{{dsc h2|Wide string manipulation}}
{{dsc inc | cpp/string/multibyte/dsc mblen}}
+
{{dsc inc|cpp/string/multibyte/dsc mblen}}
{{dsc inc | cpp/string/multibyte/dsc mbtowc}}
+
{{dsc inc|cpp/string/multibyte/dsc mbtowc}}
{{dsc inc | cpp/string/multibyte/dsc wctomb}}
+
{{dsc inc|cpp/string/multibyte/dsc wctomb}}
{{dsc inc | cpp/string/multibyte/dsc mbstowcs}}
+
{{dsc inc|cpp/string/multibyte/dsc mbstowcs}}
{{dsc inc | cpp/string/multibyte/dsc wcstombs}}
+
{{dsc inc|cpp/string/multibyte/dsc wcstombs}}
  
{{dsc h2 | Miscellaneous algorithms and math}}
+
{{dsc h2|Miscellaneous algorithms and math}}
{{dsc inc | cpp/numeric/random/dsc rand}}
+
{{dsc inc|cpp/numeric/random/dsc rand}}
{{dsc inc | cpp/numeric/random/dsc srand}}
+
{{dsc inc|cpp/numeric/random/dsc srand}}
{{dsc inc | cpp/algorithm/dsc qsort}}
+
{{dsc inc|cpp/algorithm/dsc qsort}}
{{dsc inc | cpp/algorithm/dsc bsearch}}
+
{{dsc inc|cpp/algorithm/dsc bsearch}}
{{dsc inc | cpp/numeric/math/dsc abs}}
+
{{dsc inc|cpp/numeric/math/dsc abs}}
{{dsc inc | cpp/numeric/math/dsc div}}
+
{{dsc inc|cpp/numeric/math/dsc div}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 65: Line 61:
 
{{source|1=
 
{{source|1=
 
namespace std {
 
namespace std {
   using size_t = /* see description */;
+
   using size_t = /* see description */; // freestanding
   using div_t = /* see description */;
+
   using div_t =   /* see description */; // freestanding
   using ldiv_t = /* see description */;
+
   using ldiv_t = /* see description */; // freestanding
   using lldiv_t = /* see description */;
+
   using lldiv_t = /* see description */; // freestanding
 
}
 
}
  
#define NULL /* see description */
+
#define NULL         /* see description */ // freestanding
#define EXIT_FAILURE /* see description */
+
#define EXIT_FAILURE /* see description */ // freestanding
#define EXIT_SUCCESS /* see description */
+
#define EXIT_SUCCESS /* see description */ // freestanding
#define RAND_MAX /* see description */
+
#define RAND_MAX     /* see description */
#define MB_CUR_MAX /* see description */
+
#define MB_CUR_MAX   /* see description */
  
 
namespace std {
 
namespace std {
 
   // Exposition-only function type aliases
 
   // Exposition-only function type aliases
   extern "C" using /*c-atexit-handler*/ = void();                       // exposition only
+
   extern "C" using /* c-atexit-handler */ = void(); // exposition only
   extern "C++" using /*atexit-handler*/ = void();                       // exposition only
+
   extern "C++" using /* atexit-handler */ = void(); // exposition only
   extern "C" using /*c-compare-pred*/ = int(const void*, const void*);   // exposition only
+
   extern "C" using /* c-compare-pred */ =           // exposition only
   extern "C++" using /*compare-pred*/ = int(const void*, const void*);   // exposition only
+
    int(const void*, const void*);
 +
   extern "C++" using /* compare-pred */ =           // exposition only
 +
    int(const void*, const void*);
  
 
   // start and termination
 
   // start and termination
   [[noreturn]] void abort() noexcept;
+
   [[noreturn]] void abort() noexcept;                       // freestanding
   int atexit(/*c-atexit-handler*/* func) noexcept;
+
   int atexit(/* c-atexit-handler */* func) noexcept;       // freestanding
   int atexit(/*atexit-handler*/* func) noexcept;
+
   int atexit(/* atexit-handler */* func) noexcept;         // freestanding
   int at_quick_exit(/*c-atexit-handler*/* func) noexcept;
+
   int at_quick_exit(/* c-atexit-handler */* func) noexcept; // freestanding
   int at_quick_exit(/*atexit-handler*/* func) noexcept;
+
   int at_quick_exit(/* atexit-handler */* func) noexcept;   // freestanding
   [[noreturn]] void exit(int status);
+
   [[noreturn]] void exit(int status);                       // freestanding
   [[noreturn]] void _Exit(int status) noexcept;
+
   [[noreturn]] void _Exit(int status) noexcept;             // freestanding
   [[noreturn]] void quick_exit(int status) noexcept;
+
   [[noreturn]] void quick_exit(int status) noexcept;       // freestanding
  
 
   char* getenv(const char* name);
 
   char* getenv(const char* name);
Line 124: Line 122:
  
 
   // C standard library algorithms
 
   // C standard library algorithms
   void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
+
   void* bsearch(const void* key, const void* base, // freestanding
                /*c-compare-pred*/* compar);
+
                size_t nmemb, size_t size, /* c-compare-pred */* compar);
   void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
+
   void* bsearch(const void* key, const void* base, // freestanding
                /*compare-pred*/* compar);
+
                size_t nmemb, size_t size, /* compare-pred */* compar);
   void qsort(void* base, size_t nmemb, size_t size, /*c-compare-pred*/* compar);
+
   void qsort(void* base, size_t nmemb, size_t size, // freestanding
   void qsort(void* base, size_t nmemb, size_t size, /*compare-pred*/* compar);
+
            /* c-compare-pred */* compar);
 +
   void qsort(void* base, size_t nmemb, size_t size, // freestanding
 +
            /* compare-pred */* compar);
  
 
   // low-quality random number generation
 
   // low-quality random number generation
Line 136: Line 136:
  
 
   // absolute values
 
   // absolute values
   constexpr int abs(int j);
+
   constexpr int abs(int j);                       // freestanding
   constexpr long int abs(long int j);
+
   constexpr long int abs(long int j);             // freestanding
   constexpr long long int abs(long long int j);
+
   constexpr long long int abs(long long int j);  // freestanding
   constexpr float abs(float j);
+
   constexpr /* floating-point-type */
   constexpr double abs(double j);
+
    abs(/* floating-point-type */ j);             // freestanding-deleted
  constexpr long double abs(long double j);
+
  
   constexpr long int labs(long int j);
+
   constexpr long int labs(long int j);           // freestanding
   constexpr long long int llabs(long long int j);
+
   constexpr long long int llabs(long long int j); // freestanding
  
   constexpr div_t div(int numer, int denom);
+
   constexpr div_t div(int numer, int denom);                         // freestanding
   constexpr ldiv_t div(long int numer, long int denom);
+
   constexpr ldiv_t div(long int numer, long int denom);             // freestanding
   constexpr lldiv_t div(long long int numer, long long int denom);
+
   constexpr lldiv_t div(long long int numer, long long int denom);   // freestanding
   constexpr ldiv_t ldiv(long int numer, long int denom);
+
   constexpr ldiv_t ldiv(long int numer, long int denom);             // freestanding
   constexpr lldiv_t lldiv(long long int numer, long long int denom);
+
   constexpr lldiv_t lldiv(long long int numer, long long int denom); // freestanding
 
}
 
}
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=286|std=C++98|before=the definition of {{tt|size_t}} was not provided in {{tt|<cstdlib>}}|after=provided}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===

Latest revision as of 23:53, 12 September 2023

 
 
Standard library headers
 

This header was originally in the C standard library as <stdlib.h>.

This header provides miscellaneous utilities. Symbols defined here are used by several library components.

Contents

Types

structure type, returned by std::div
(typedef) [edit]
structure type, returned by std::ldiv
(typedef) [edit]
(C++11)
structure type, returned by std::lldiv
(typedef) [edit]
unsigned integer type returned by the sizeof operator
(typedef) [edit]

Macro constants

indicates program execution status
(macro constant) [edit]
MB_CUR_MAX
maximum number of bytes in a multibyte character with the current locale
(macro constant) [edit]
implementation-defined null pointer constant
(macro constant) [edit]
maximum possible value generated by std::rand
(macro constant) [edit]

Functions

Process control
causes abnormal program termination (without cleaning up)
(function) [edit]
causes normal program termination with cleaning up
(function) [edit]
causes quick program termination without completely cleaning up
(function) [edit]
(C++11)
causes normal program termination without cleaning up
(function) [edit]
registers a function to be called on std::exit() invocation
(function) [edit]
registers a function to be called on std::quick_exit invocation
(function) [edit]
calls the host environment's command processor
(function) [edit]
access to the list of environment variables
(function) [edit]
Memory management
allocates memory
(function) [edit]
allocates aligned memory
(function) [edit]
allocates and zeroes memory
(function) [edit]
expands or shrinks previously allocated memory block
(function) [edit]
deallocates previously allocated memory
(function) [edit]
Numeric string conversion
converts a byte string to a floating point value
(function) [edit]
converts a byte string to an integer value
(function) [edit]
converts a byte string to an integer value
(function) [edit]
converts a byte string to an unsigned integer value
(function) [edit]
converts a byte string to a floating-point value
(function) [edit]
Wide string manipulation
returns the number of bytes in the next multibyte character
(function) [edit]
converts the next multibyte character to wide character
(function) [edit]
converts a wide character to its multibyte representation
(function) [edit]
converts a narrow multibyte character string to wide string
(function) [edit]
converts a wide string to narrow multibyte character string
(function) [edit]
Miscellaneous algorithms and math
generates a pseudo-random number
(function) [edit]
seeds pseudo-random number generator
(function) [edit]
sorts a range of elements with unspecified type
(function) [edit]
searches an array for an element of unspecified type
(function) [edit]
computes absolute value of an integral value (|x|)
(function) [edit]
computes quotient and remainder of integer division
(function) [edit]

[edit] Synopsis

namespace std {
  using size_t =  /* see description */; // freestanding
  using div_t =   /* see description */; // freestanding
  using ldiv_t =  /* see description */; // freestanding
  using lldiv_t = /* see description */; // freestanding
}
 
#define NULL         /* see description */ // freestanding
#define EXIT_FAILURE /* see description */ // freestanding
#define EXIT_SUCCESS /* see description */ // freestanding
#define RAND_MAX     /* see description */
#define MB_CUR_MAX   /* see description */
 
namespace std {
  // Exposition-only function type aliases
  extern "C" using /* c-atexit-handler */ = void(); // exposition only
  extern "C++" using /* atexit-handler */ = void(); // exposition only
  extern "C" using /* c-compare-pred */ =           // exposition only
    int(const void*, const void*);
  extern "C++" using /* compare-pred */ =           // exposition only
    int(const void*, const void*);
 
  // start and termination
  [[noreturn]] void abort() noexcept;                       // freestanding
  int atexit(/* c-atexit-handler */* func) noexcept;        // freestanding
  int atexit(/* atexit-handler */* func) noexcept;          // freestanding
  int at_quick_exit(/* c-atexit-handler */* func) noexcept; // freestanding
  int at_quick_exit(/* atexit-handler */* func) noexcept;   // freestanding
  [[noreturn]] void exit(int status);                       // freestanding
  [[noreturn]] void _Exit(int status) noexcept;             // freestanding
  [[noreturn]] void quick_exit(int status) noexcept;        // freestanding
 
  char* getenv(const char* name);
  int system(const char* string);
 
  // C library memory allocation
  void* aligned_alloc(size_t alignment, size_t size);
  void* calloc(size_t nmemb, size_t size);
  void free(void* ptr);
  void* malloc(size_t size);
  void* realloc(void* ptr, size_t size);
 
  double atof(const char* nptr);
  int atoi(const char* nptr);
  long int atol(const char* nptr);
  long long int atoll(const char* nptr);
  double strtod(const char* nptr, char** endptr);
  float strtof(const char* nptr, char** endptr);
  long double strtold(const char* nptr, char** endptr);
  long int strtol(const char* nptr, char** endptr, int base);
  long long int strtoll(const char* nptr, char** endptr, int base);
  unsigned long int strtoul(const char* nptr, char** endptr, int base);
  unsigned long long int strtoull(const char* nptr, char** endptr, int base);
 
  // multibyte / wide string and character conversion functions
  int mblen(const char* s, size_t n);
  int mbtowc(wchar_t* pwc, const char* s, size_t n);
  int wctomb(char* s, wchar_t wchar);
  size_t mbstowcs(wchar_t* pwcs, const char* s, size_t n);
  size_t wcstombs(char* s, const wchar_t* pwcs, size_t n);
 
  // C standard library algorithms
  void* bsearch(const void* key, const void* base,  // freestanding
                size_t nmemb, size_t size, /* c-compare-pred */* compar);
  void* bsearch(const void* key, const void* base,  // freestanding
                size_t nmemb, size_t size, /* compare-pred */* compar);
  void qsort(void* base, size_t nmemb, size_t size, // freestanding
             /* c-compare-pred */* compar);
  void qsort(void* base, size_t nmemb, size_t size, // freestanding
             /* compare-pred */* compar);
 
  // low-quality random number generation
  int rand();
  void srand(unsigned int seed);
 
  // absolute values
  constexpr int abs(int j);                       // freestanding
  constexpr long int abs(long int j);             // freestanding
  constexpr long long int abs(long long int j);   // freestanding
  constexpr /* floating-point-type */
    abs(/* floating-point-type */ j);             // freestanding-deleted
 
  constexpr long int labs(long int j);            // freestanding
  constexpr long long int llabs(long long int j); // freestanding
 
  constexpr div_t div(int numer, int denom);                         // freestanding
  constexpr ldiv_t div(long int numer, long int denom);              // freestanding
  constexpr lldiv_t div(long long int numer, long long int denom);   // freestanding
  constexpr ldiv_t ldiv(long int numer, long int denom);             // freestanding
  constexpr lldiv_t lldiv(long long int numer, long long int denom); // freestanding
}

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 286 C++98 the definition of size_t was not provided in <cstdlib> provided

[edit] See also