How to Use Bad_Alloc in C++

When you do some code, there is a possibility of making some mistakes, i.e., syntax or logic. Within programming, we call such mistakes errors or exceptions. These errors or exceptions could cause your program to stop at any point while execution. From all of these exceptions, one of them is the bad memory allocation exception in C++. The ad_alloc named built-in exception class has been found and used in C++ to get the memory allocation exceptions. This bad memory allocation exception can be caused by the bad_Alloc class used within the “try-catch” statement of C++. So, within this guide today, we will be discussing how to get the bad memory error in C++ while using the bad_alloc class in the code. So, let’s have a quick look.

We have been starting this article by opening a smart terminal of the Ubuntu 20.04 Linux system.  This could be done with the usage of the “Ctrl+Alt+T” simple shortcut key. After that, we have to create a fully new c++ file as we need to do some coding. This creation must be done within the terminal, and the “touch” command used for this purpose is already shown in the attached image. You have to use the “.cc” extension with the name of a file in making it, opening it, and compiling it. After the quick creation of the C++ “badalloc.cc” file, we are ready to open it within the GNU nano editor that comes built-in in Ubuntu 20.04. You can try to open it within some other editor in the Ubuntu 20.04 system as well, i.e., text editor. But, we prefer to use the “GNU” by using the “nano” instruction as demonstrated.

Example 01:

Let’s have a first simple example for bad_alloc usage in C++. It’s high time to write a C++ code in the opened empty file in Nano editor. So, within the bad allocation exception code, we need headers. One of them is every known “input-output” stream, used to get the standard input and show the standard output on the screen. The other one is the “new” header, which will be used in the code to allocate memory to some pointer variable. This bad memory allocation error occurs most of the time when we tend to use the “new” keyword in the code to allocate memory. The execution of this c++ code will be started from the main function of integer type. We have been using the try-catch statement in the c++ code. The try part is used to add the code having a bad memory allocation error in it. The “catch” part is used to just display the error type with description without stopping or halting the execution of the code suddenly. Within the try part, we have initialized an integer type pointer “A” and assigned it a huge memory with the help of a “new” keyword. As this memory has been assigned with the help of a “new” keyword, it would cause an error and stop the program while executing.

To avoid the sudden stopping of this program and make it execute the other parts of the code as well, we have used the “catch” statement. We used the standard bad_alloc class to get the bad memory allocation error within the catch statement, i.e., “std::bad_alloc”. The inner structure of the catch statement will decide what to do after the exception has been found. So, we have used the standard “cerr” statement of C++ to display the error on the shell with the help of an exception object “e”. This can be done by calling the “what” function with the object “e”. The error title will be shown in the terminal application of your system upon the execution. The program ends here, and we are ready to make it execute. Let’s just quickly save our newly made code with the use of a simple “Ctrl+S” shortcut and the “Ctrl+X” to close this saved file as well.

Let’s just do some compilation to make this c++ code executable and error-free as well. So, we recommend configuring the “g++” compiler of the C++ language in the Ubuntu 20.04 system. So, we have been using the “g++” as well. The compilation is successful. After running the code file using the “./a.out” instruction, we got the bad_alloc standard exception as a display message. The code continued to execute and didn’t stop after getting an error.

Example 02:

Let’s have another example of the bad_alloc class in the code to get the bad memory allocation error in the catch exception. The same header files have been utilized. Within the main function, we have been utilizing the same try-catch statement. Within the try statement of code, we have been using the “while” statement. It is using the truth as its condition. Until the system has memory and condition is satisfied, the “new” keyword will continue to allocate the memory. But no pointer or variable has been used for the memory allocation to it. The catch statement has been again used here to display the memory allocation error in the terminal as a simple display message. The standard “bad_alloc” class has been getting the memory allocation bad error within the object “e”. This error would then be displayed on the shell with the usage of a cout standard statement. This statement has been simple, calling the what() function of the diverse C++ language with the “e” object of class “bad_alloc”.

The compilation was quite successful as it returned no syntax errors. The execution has been displaying that our code has got the bad_alloc memory allocation error.

Example 03:

Within our last example, we will see how we can avoid the bad memory allocation error. We have been using the “vector” header and the “string” header as well. The main function contains the try-catch statement. The try statement first contains the “vector” type character iterator named “v” and allocates memory to it. The standard “cout” statement is used here to display the success message that the memory has been allocated successfully. The catch statement is used here to get the exception of bad allocation and do some tasks after that. It again contains the iterator vector “s” of a list type. After that, the cout statement has been used here to display the list “s” content from start to end using the “begin()” and “end” function with “s”. It also displays the exception that may or may not be caused by the try statement with the exception object “e” with the “what” function.

As the memory has been allocated successfully, no catch statement has been executed. Therefore, we have got the “success” message on our shell.


This article will guide you to use the “bad_alloc” class in C++ to get the bad memory allocation error in the code. We have discussed it using the try-catch statement in the code. This was done to avoid the sudden quitting of code during the execution. We have also discussed how to avoid this error using the exception object of class “bad_alloc”. To sum up, this guide will be a bundle of examples to make you understand the bad_alloc error.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.