C++ Reference ยท Memory and Utilities
The <cstdlib> header
Reference for the C++ cstdlib header: integer abs, div, string-to-number conversion, exit and abort, atexit, getenv, qsort, bsearch and rand.
<cstdlib> is the C++ wrapper for C's <stdlib.h>. It collects general-purpose facilities: integer absolute value and division, converting text to numbers, ending the program, reading environment variables, running a command, the C-style qsort and bsearch, and the old rand generator.
Most of it has better C++ replacements. Use std::from_chars or std::stoi instead of atoi, <random> instead of rand, std::sort instead of qsort, and std::vector instead of malloc. A few parts remain the normal choice: std::exit, std::abort, std::getenv and std::abs for integers.
#include <cstdlib> int n = std::abs(-7); // 7
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
int abs(int); long labs(long); long long llabs(long long); div_t div(int num, int den); long strtol(const char* s, char** end, int base); double strtod(const char* s, char** end); [[noreturn]] void exit(int status); [[noreturn]] void abort(); int atexit(void (*f)()); char* getenv(const char* name); void qsort(void* base, size_t n, size_t size, int (*cmp)(const void*, const void*)); void* bsearch(const void* key, const void* base, size_t n, size_t size, int (*cmp)(const void*, const void*)); int rand(); void srand(unsigned seed); constexpr int RAND_MAX = /* implementation-defined */;
| Group | Notes |
|---|---|
| Integer math | abs, labs, llabs, div, ldiv, lldiv. abs of the smallest negative value is undefined. |
| Conversion | atoi, atol, atof give no error signal; strtol, strtoul, strtod report where parsing stopped and set errno to ERANGE on overflow. |
| Termination | exit runs static destructors and atexit handlers; _Exit and quick_exit skip some of that; abort ends at once with a failure signal. |
| Environment | getenv returns nullptr when the variable is missing; system runs a shell command. |
| Sorting | qsort, bsearch through comparison callbacks. |
| Random | rand, srand. |
Returns / errors: the conversion functions return 0 on failure for the ato* family; getenv returns a pointer you must not modify; qsort with an inconsistent comparison function is undefined behavior.
Usage notes
exitdoes not unwind the stack. Local objects of the calling functions are not destroyed. Return frommaininstead when possible.abortis for unrecoverable failure (it raisesSIGABRT). It does not run destructors oratexithandlers.qsortworks on trivially copyable types only, is typically slower thanstd::sortbecause the comparison is a function pointer, and is not stable.rand()has hidden global state and a sequence of unspecified quality. Its values differ between platforms, and the standard does not require it to be thread-safe. Use<random>.std::absoverloads for floating-point are in<cmath>. Including only<cstdlib>and callingabs(2.5)can pick the integer version on some platforms.system()is a security risk when part of the command string comes from users.malloc,calloc,freeare also here, but use smart pointers and containers.
Related
<cmath>,std::from_chars, string conversions,<random>- Lessons: Exceptions, Math functions