Reasons for a C++ program crash Last Updated : 07 Mar, 2023 Comments Improve Suggest changes Like Article Like Report We sometimes come across abnormal crash of C++ programs. Below are some possible reasons which may cause C++ to crash abnormally. Segmentation Fault: It is the major reason for program to crash. These are may be the reasons for the such cause:Attempting to access memory location that doesn't exist in our system.There may be an attempt to write on a read only memory location. CPP // CPP program to demonstrate int main() { char *str; /* Stored in read only part of data segment */ str = "GfG"; /* Problem: trying to modify read only memory */ *(str+1) = 'n'; return 0; } Output :Segmentation fault (core dumped)There may be an attempt to access protected memory location such as kernel memoryStack Overflow: There may case of non terminating recursion with memory location. CPP // C program to demonstrate stack overflow // by creating a non-terminating recursive // function. #include<stdio.h> void fun(int x) { if (x == 1) return; x = 6; fun(x); } int main() { int x = 5; fun(x); } Output :Segmentation fault (core dumped)Buffer Overflow: It is an anomaly where a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory locations.Consider below C++ program. CPP // C++ code to demonstrate buffer // overflow. #include <bits/stdc++.h> using namespace std; // driver code int main() { char A[8] = ""; unsigned short B = 1979; strcpy(A, "excessive"); return 0; } Output :*** stack smashing detected ***: /home/gfg/a terminated Aborted (core dumped)The program has two variables which are adjacent in memory: an 8-byte-long string buffer, A, and a two-byte big-endian integer, B. char A[8] = ""; unsigned short B = 1979; Initially, A contains nothing but zero bytes, and B contains the number 1979. Now, the program attempts to store the null-terminated string "excessive" with ASCII encoding in the A buffer. strcpy(A, "excessive"); This string is of 9 characters long and encodes to 10 bytes including the null terminator, but A can take only 8 bytes. By failing to check the length of the string, it also overwrites the value of B: B's value has now been inadvertently replaced by a number formed from part of the character string. In this example "e" followed by a zero byte would become 25856. This overflow is called buffer overflow.Memory Leak: If we allocate some memory by some program and let it be as it is. After sometime there will be huge memory that is allocated but not used so this would lack of memory after sometime. And thus program start crashing. CPP // C program to demonstrate heap overflow // by continuously allocating memory #include<stdio.h> int main() { for (int i=0; i<10000000; i++) { // Allocating memory without freeing it int *ptr = (int *)malloc(sizeof(int)); } } ExceptionsDivide by zero. CPP // C++ code to demonstrate divide by 0. #include <bits/stdc++.h> using namespace std; // driver code int main() { int x = 10; int y = 0; cout << x/y; return 0; } Output :Floating point exception (core dumped) Comment More infoAdvertise with us Next Article Reasons for a C++ program crash A akash1295 Follow Improve Article Tags : Misc C++ Programs C++ system-programming Practice Tags : CPPMisc Similar Reads C++ Program to Create a File Problem Statement:Write a C++ program to create a file using file handling and check whether the file is created successfully or not. If a file is created successfully then it should print "File Created Successfully" otherwise should print some error message. Approach:Declare a stream class file and 2 min read C++ Program to Show Types of Errors In any programming language errors is common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code. In a similar way the errors are classified as below: Syntax Errors Runtime Errors Logical Errors Link 3 min read Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo 5 min read C++ Program to Show Use of This Keyword in Class Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. There are 4 wa 4 min read C++ Program to Show Runtime Exceptions A runtime error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. Here, we will learn how to handle runtime exceptions in C++. There are 5 types of runtime exceptions discussed here: Division by zero. Segmentation faults. La 3 min read Like