Updated April 15, 2023
Introduction to C++ stack overflow
The following article provides an outline for C++ stack overflow. Stack overflow is a software bug which occurs when a program tries to access more memory than the available stack size, which results in the crashing of the program. Stack is Last in First Out data structure (LIFO). It is used to store the local variables, parameters/ arguments passed in function, and their return addresses. When the function runs completely, all its local variables and other data are deleted from the stack, and the memory is freed. However, as the stack memory is limited in the computer, if the program accesses more memory than this available one, the stack overflow condition arises.
Syntax of C++ stack overflow
There is no specific syntax of stack overflow as this is a runtime bug in a program and can cause at any time because any function call or a large number of local variables occupying the stack memory can be responsible for it.
But generally, the condition of stack overflow arises in 2 cases which are mentioned below:
1. When the function calls itself repeatedly/ recursively (infinite times) without stopping, which causes all the function data (local variables, parameters, and return addresses) stacked up, and the available stack cannot store it.
void function_f()
{
function_f();
}
int main()
{
function_f();
return 0;
}
2. When a large number of local variables are declared, or there is a very large array declared in the program resulting in the overflow of stack memory.
int function_f()
{
int array[100000001];
}
How does stack overflow work in C++?
- As said earlier, stack data structure follows the LIFO (Last In First Out) mechanism and is used to call the user subroutines. It works similar to the dinner plates placed one above the other. So, when these plates are required, the last plate is used first, and the first plate set is used at last. The same thing happens in the case of stack memory, as the stack is the region of the process’s memory and is limited in the computer.
- A function/ method in C++ code has local variables, parameters, and return addresses defined. When the function is called recursively, all the related things of that function (local variables, parameters, etc.) get stored in the stack. But when there is some issue in the logic of the program causing that function to be called infinite times, pushing the function call-related data in the stack memory. As a result, none of the function calls will get executed so that no stack memory will be freed up. So, the stack becomes full, and the program will try to access more memory than available, resulting in the condition of stack overflow.
- One other reason for the overflowing is that when the programmer defines the array or a matrix with a very large length in a program, which will also access more memory of the stack than the limit, causing the stack overflow. In this case, the program is not able to perform any task (executing functions, processing arrays of large size), and the Operating System takes the control back, clears all the stack data, and then crashes the program (allows the termination of the program). Though stack overflow depends on various factors like the computer architecture, available system memory, language in which the program is written, etc.
Examples of C++ stack overflow
Given below are the examples of C++ stack overflow:
Example #1
When there are recursive function calls which causes the infinite loop resulting in a stack overflow.
Code:
#include <iostream>
using namespace std;
// function which checks whether the number is even or not
void check_even(int num)
{
int result;
result = num%2;
// checking the condition of even number
if (result ==0)
{
cout << "number is even" << endl;
}
else
{
// recursively calls the above function when the number is odd by incrementing its value by 2
check_even(num+2);
}
}
int main ()
{
// calling the function with an odd parameter ‘3’ passed in it
check_even(3);
return 0;
}
Output:
Explanation:
- stack overflow can also cause the segmentation fault, which indicates that the program is trying to access the memory, which is either not available or has no permission to. The above program, as the parameter passed in the function call is 3, is odd. When the function is called, or the control moves to the function, and check whether 3%2 == 0.
- Since it becomes false, it will move to the else part, and the calling of the function check_even(3+2) = check_even(5) is made. Every time the value passed to the function is odd, and it is called recursively with all the local variables and parameters stack one above the other resulting in the overflowing of stack memory.
Example #2
When the size of the array declared by the programmer is very large.
Code:
#include <iostream>
#include <array>
using namespace std;
int main ()
{
// declaring array with size defined
int big_array [100000001];
// for loop used to assign the value to array elements
for (int i =0; i< 100000001; i++)
{
big_array[i] = i+100;
}
// for loop used to print the values to array elements
for (int i= 0; i< 100000001; i++)
{
cout << big_array[i] << endl;
}
return 0;
}
Output:
Explanation:
- In the code above, an array with the name big array is declared. Then, the value of the array elements is assigned using the basic for loop and is printed on the console using the for loop again.
- But, the size of the array declared by the programmer is 100000001, which is quite large and hence overflows the stack memory. Available stack memory cannot store such a large array, so it indirectly results in the segmentation fault or stack overflow.
Conclusion
The above description clearly shows what is stack overflow exception in C++ and how it works in a program. Although it throws an asynchronous exception, and these exceptions can be handled in C++ using the throws keyword, but stack overflow can not be handled in this way as any function call, or stack allocation can cause the overflowing of the stack at any time. So needs to keep in mind to program accordingly in the order it would not cause this bug as it leads to abrupt crashing of the program.
Recommended Articles
This is a guide to C++ stack overflow. Here we discuss the introduction, how stack overflow works in C++? and examples, respectively. You may also have a look at the following articles to learn more –