C++ Library - <cstdlib>



The <cstdlib> library in C++ provides a variety of functions for performing common operations, such as memory management, random number management and string conversion. It is a part of the C standard library, that is useful when dealing with the low level operations.

In C++ <cstdlib> is used when interacting with C-style operations or dealing with low-level memory management. While C++ has its own alternatives for memory allocations and type conversions, the functions provided by the <cstdlib> are still widely used in certain contexts.

Including <cstdlib> Header

To include the <cstdlib> header in your C++ program, you can use the following syntax.

#include <cstdlib>

Functions of <cstdlib> Header

Below is list of all functions from <cstdlib> header.

String Conversion

These functions are used for converting strings (C-style strings) to numerical values such as integers and floating numbers or to convert numbers to strings. The commonly used C++ <cstdlib> string conversion functions are listed below along with their description.

Sr.NoFunctions & Description
1atof

It converts a string to a floating point value.

2atoi & atol & atoll

It converts a string to an integer value.

3strtoul & strtoull

It converts a string to a unsigned integer value.

4strtof & strtod & strtold

It converts a string to a floating point value.

String to Integer Conversion

In the following example, we are going to use the atoi to convert the string to an integer.

#include <iostream>
#include <cstdlib>
int main() {
   const char * x = "11123";
   int y = std::atoi(x);
   std::cout << "Result : " << y << std::endl;
   return 0;
}

Output

Output of the above code is as follows −

Result : 11123

Process Control

These functions are used to manage and control program executions. They include operations such as program termination, environment manipulation, and process handling. The commonly used C++ <cstdlib> process control functions are listed below along with their description.

Sr.NoFunctions & Description
1abort

It causes the program termination.

2exit

It causes the program termination with cleaning up.

3atexit

It registers a function to be called on the exit().

4quick_exit

It causes the program termination without completely cleaning up.

5at_quick_exit

It registers a function to be called on the quick_exit().

6_Exit

It is used to terminate the calling process.

Terminating Program

Let's look at the following example, where we are going to terminate the program immediately.

#include <cstdlib>
#include <iostream>
int main() {
   std::cout << "Program Started" << std::endl;
   if (true) {
      std::cout << "Exiting Program..." << std::endl;
      std::exit(0);
   }
   std::cout << "Hello" << std::endl;
   return 0;
}

Output

Following is the output of the above code −

Program Started
Exiting Program..

Memory Management

Memory management refers to the process of allocating and deallocating of the memory. The commonly used C++ <cstdlib> memory management functions are listed below along with their description.

Sr.NoFunctions & Description
1calloc

It is used to allocates memory for an array and initializes all to zero.

2free

It is used to deallocates the previously allocated memory .

3malloc

It is used to allocate the memory.

4realloc

It is used to expands or shrinks previously allocated memory.

Memory Allocation

Consider the following example, where we are going to allocate a block a memory dynamically.

#include <iostream>
#include <cstdlib>
int main() {
   int * x = (int * ) std::malloc(4 * sizeof(int));
   if (x == nullptr) {
      std::cerr << "Memory Allocation Failed." << std::endl;
      return 1;
   }
   for (int a = 0; a < 4; ++a) {
      x[a] = a * 2;
      std::cout << x[a] << " ";
   }
   std::cout << std::endl;
   std::free(x);
   return 0;
}

Output

If we run the above code it will generate the following output −

0 2 4 6