C/C++V Error segmentation

August 2016

When developing an application in C/C++ under Linux, when launching the application for testing, you received below error message:

Segmentation fault

However with compiled language like c or c++, your computer progam is no more under supervision of interpreter or virtual machine to detect from where the error comes from.

Debuggers are the only available application to trouble shoot the error.

Under OS like Windows NT/XP with Linux, it allocates part of the memory for each application. If an unknown application tries to integrate a memory zone, same will be detected directly and stop all application which will generate an error segmentation under linux.

Under C

Take an example a simple C program who voluntarily crash generating error segmentation:

void launch_error_segmentation()
	int *hazardous_pointer=(int *) 100;
	int test=*hazardous_pointer;

int main(int argc, char ** argv)
	return 0;

Under launch_error_segmentation, the hazardous_pointer points to adress 100 in the memory.

This address cannot belong to a normal application.

When hazardous_pointer will try to read the memory address 100 to put the value for this addresse in the variable test, this will crash the program and will generate an error segmentation.

To debug this program, first of all, compile the debugging symbols. This will load the function names and variable once the program compiled and then debug to detect the error by indicating the function names.

Below compiling:

gcc -g test.c -o test

Program launched for debugging
gdb ./test

Below command will appear:



Below will appear:
(gdb) run

Starting program: /home/chantecode/Desktop/test

Program received signal SIGSEGV, Segmentation fault.
0x08048334 in launch_error_segmentation () at test.c:4
4               int test=*hazardous_pointer;

The debugger will indicate the error line in the file source :

int test=*hazardous_pointer;

Type the command bt to detect the error source:
(gdb) bt
#0  0x08048334 in launch_error_segmentation () at test.c:4
#1  0x0804834e in main () at test.c:9

Under C++

For error segmentation under C++, it is quite the same as for language C but however we should use the command g++ with option -g like with gcc.

The gdb report will be detailed as below:

//File test.cpp
class Test{
		int to;
		int incremente_a(){ a++; };

int main()
	Test *t;
	t=(Test *)100;
	return 0;

compiled as below:
g++ -g test.cpp -o test

Launch debugger
gdb ./test

Type the same command as below :
(gdb) run

Starting program: /home/chantecode/Desktop/test

Program received signal SIGSEGV, Segmentation fault.
0x080483fc in Test::incremente_a (this=0x64) at test.cpp:7
7                       int incremente_a(){ a++; };
(gdb) bt
#0  0x080483fc in Test::incremente_a (this=0x64) at test.cpp:7
#1  0x080483e7 in main () at test.cpp:14

See also :

This document entitled « C/C++V Error segmentation » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.