How to find Segmentation Error in C & C++ ? (Using GDB) Last Updated : 03 Sep, 2018 Comments Improve Suggest changes Like Article Like Report What is Segmentation Error ? - It is the runtime error caused because of the memory access violation. For Eg :-Stackoverflow, read violation etc.. We often face this problem when working out with pointers in c++/c. In this example we will see how to find the segmentation error in the program. We will find which lines causes the segmentation fault error. Note :- I have used Linux distro - Ubuntu for this demonstration. So, Consider the following snippet of C++ Code. CPP // Segmentation Error Demonstration // Author - Rohan Prasad #include <iostream> using namespace std; int main() { int* p = NULL; // This lines cause the segmentation // error because of accessing the // unknown memory location. *p = 1; cout << *p; return 0; } How to find that error using gdb? Let's say your file name is saved as Program1.cpp. Head our to your terminal (Be in the directory in which this Program1.cpp is available) Step 1: Compile it. $ gcc -g Program1.cpp (in my case). Step 2: Run it. $ ./a.out (it is Object File) If it shows Segmentation fault (core dumped) then follow following steps. Step 3:Debug it $ gdb ./a.out core Your output will look like something this: ------------------------------------------------------------------------------------ GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./a.out...done. /home/logarithm/Desktop/Test Case/Miccl/core: No such file or directory. (gdb) ------------------------------------------------------------------------------------ Then just type r and press the enter key . The output will be something like this showing the erroneous statement. ----------------------------------------------------------------------------------- (gdb) r Starting program: /home/logarithm/Desktop/Test Case/Miccl/a.out Program received signal SIGSEGV, Segmentation fault. 0x00005555555547de in main () at Sege.cpp:8 8 *p=1; (gdb) ------------------------------------------------------------------------------------ Now you have got the line that causes segmentation error. Exit from debugger and correct the program. For exiting type quit and press enter. ----------------------------------------------------------------------------------- (gdb) quit A debugging session is active. Inferior 1 [process 3617] will be killed. Quit anyway? (y or n) y ----------------------------------------------------------------------------------- So, wow you have resolved the head torturing segmentation fault. Comment More infoAdvertise with us Next Article How to find Segmentation Error in C & C++ ? (Using GDB) L Logarithm Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads How to Use FlawFinder-python Tool to Find Vulnerabilities in C/C++ Code? FlawFinder is a python based tool that helps in finding vulnerabilities in a C/C++ source code. It examines the source code and gives the list of possible vulnerabilities/flaws in the code as the output. Installation There is a pre-packaged version of this tool for Unix systems like Debian, Fedora, 3 min read Segmentation Fault in C++ Segmentation faults C++ is an error that occurs when a program attempts to access a memory location it does not have permission to access. Generally, this error occurs when memory access is violated and is a type of general protection fault. Segfaults are the abbreviation for segmentation faults.Tab 7 min read Elements present in first array and not in second using STL in C++ Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array, using STL in C++ Examples: Input: a[] = {1, 2, 3, 4, 5, 10}, b[] = {2, 3, 1, 0, 5} Output: 4 10 Input:a[] = {4, 3, 5, 9, 11}, b[] = {4, 9, 3, 11, 10}; Output: 5 Approach: In STL, 2 min read Write a C program that won't compile in C++ Although C++ is designed to have backward compatibility with C, there can be many C programs that would produce compiler errors when compiled with a C++ compiler. Following is the list of the C programs that wonât compile in C++: Calling a function before the declarationUsing normal pointer with con 4 min read Avoid Bugs Using Modern C++ C++ has more constructs that can cause undefined behavior or exceptions as compared to languages like Java and Python. This is because C++ was developed with a primary objective to include classes in C and hence, support bug-inviting features such as pointers, macros, etc. It is also devoid of tools 3 min read Executing main() in C/C++ - behind the scene How to write a C program to print "Hello world" without main() function? At first, it seems impractical to execute a program without a main() function because the main() function is the entry point of any program. Let us first understand what happens under the hood while executing a C program in Lin 4 min read Accessing array out of bounds in C/C++ Prerequisite: Arrays in C/C++In high level languages such as Java, there are functions which prevent you from accessing array out of bound by generating a exception such as java.lang.ArrayIndexOutOfBoundsException. But in case of C, there is no such functionality, so programmer need to take care of 2 min read functional::bad_function_call in C++ with Examples Standard C++ contains several built-in exception classes, functional::bad_function_call is one of them. This is an exception thrown on bad call. Below is the syntax for the same: Header File: include<functional> Syntax: class bad_function_call; Note: To make use of functional::bad_function_cal 1 min read ios operator() function in C++ with Examples The operator() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: operator void*() const; Parameters: This method does not accept any parameter. Return Value: This method returns a null pointer if any error bit is set of this 1 min read Errors in C/C++ Error is an illegal operation performed by the user which results in abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before c 5 min read Like