LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2007, 04:30 AM   #1
sanjuonline1
LQ Newbie
 
Registered: Feb 2007
Location: New Delhi, India
Distribution: redhat
Posts: 5

Rep: Reputation: 0
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.
 
Old 02-13-2007, 04:56 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 02-14-2007, 05:27 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Qt application: segmentation fault error dr_berta Programming 2 10-24-2005 03:20 PM
Segmentation Fault ERROR bluechicken Programming 8 06-30-2005 07:02 AM
Segmentation fault + error on bootup xushi Slackware 9 01-03-2005 05:37 PM
Segmentation Fault Error ashwinipahuja Programming 1 03-29-2004 02:09 PM
Getting segmentation fault error with Xmms Unreal7000 Linux - Software 1 06-03-2003 08:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration