LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting segmentation fault error (https://www.linuxquestions.org/questions/programming-9/getting-segmentation-fault-error-528597/)

sanjuonline1 02-13-2007 04:30 AM

getting segmentation fault error
 
Friends, I am running a c++ program on linux and it is showing segmentation fault error but same program works fine in windows. What may be the reason as I am new in programming on linux.

matthewg42 02-13-2007 04:56 AM

A segmentation violation occurs when a program tries to access memory which is not allocated for use by the program. This usually results from a program trying to use some object or pointer which has not been properly initialised, or has been corrupted somehow.

In short, your program has a bug.

You must provide more details if you would like a more precise diagnosis.

firstfire 02-14-2007 05:27 AM

Hello!

Try following steps:

1) Compile your program with `-g' option:
Code:

g++ -g <your_program>.cpp
2) run `gdb a.out'
2a) in the GDB prompt type `run':
Code:

(gdb) run <put_program_options_here>
2b) when GDB tell you that Segmentation Fault has occured:
Code:

Program received signal SIGSEGV, Segmentation fault.
0x0804836f in main () at <your_program>.cpp:<line_number>
<line_number>: ...

take a look at your code around <line_number> (type `l <line_num>'), check variables value with gdb command `print' (e.g. type `p my_variable'). It may be useful to look at stack (`bt' command, stands for backtrace) and to walk through the stack with `up' and `down' commands.

3) Another cool program is `valgrind'. Install it! Usage is very simple: `valgrind a.out' or `valgrind --leak-check=full a.out'

4) Post your code here.

Bye.


All times are GMT -5. The time now is 02:00 AM.