C/C++V Error segmentation

Last update on November 27, 2008 10:08 AM by deri58
Published by deri58

C/C++V Error segmentation






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)
{
launch_error_segmentation();
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:

(gdb)

Typerun

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{
public:
int to;
Test(){};
~Test(){};
int incremente_a(){ a++; };
};

int main()
{
Test *t;
t=(Test *)100;
t->incremente_a();
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
Best answers for « C/C++V Error segmentation » in :
How to fix a rundll error ShowHow to fix a rundll error Why does this happen? How to fix a rundll error? Solution 1: Reboot your computer Solution 2: Install and Remove the program Solution 3: Locate the error with System Configuration Solution 4: Run an...
Change the default installation folder C: \ Program Files ShowChange the default installation folder C: \ Program Files The installation of most software programs are set by default in the C: \ Program Files. Below is a means of how to change into another partition. Under Windows Vista, select...
Error Message: Disk Boot failure ShowError Message: Disk Boot failure-insert system disk and press enter Solution 1: Ensure that your drives are empty Solution 2: BIOS set up Solution 3: New hard drive configuration Solution 4: Hard drive not properly plugged in Solution 5:...
Download Windows Error Message Creator ShowThe chains which alert on viruses do not work any more, then made more extremely. If you want to scare your colleagues or your friends on their computer, the best means is of their envoy a message of error. Windows Error Message Creator is a very...
Error checking ShowError checking Binary encoding is very practical for use in electronic devices such as computers, in which information can be encoded based on whether an electrical signal is present or not. However, this electrical signal may suffer disturbances...
S-video (Y/C) ShowThe S-Video standard The S-Video standard (for "Sony Video"), sometimes called Y/C, is a video transmission mode with separate components using different cables to carry information regarding luminance (luminosity) and chrominance (colour). An S...