LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-28-2009, 02:07 PM   #1
gmo
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
Segementation fault when trying to run a working program (C++)


Okay, I know that title seems ridiculous, but here's my problem:

I wrote a C++ program that is working. I have used it a lot on my university's Linux network computers. I have had 0 problems with getting usable data out of it. Recently I transferred the program to the lab computer's in my adviser's lab. After compiling it and trying to run it, I get a segmentation fault. There is no output at all. I tried just printing a "1" before any variables were even initialized. It wouldn't even do that much before segmentation fault.

I don't think there is a problem with the code (like I said, I can get data out of the program on the university's Linux network computers). Could this be an issue with not having up-to-date C++ compiler or something of that nature?
 
Old 04-28-2009, 02:25 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by gmo View Post
I don't think there is a problem with the code (like I said, I can get data out of the program on the university's Linux network computers).
With many kinds of bug (using uninitialized memory, using an object after deleting it, clobbering unrelated memory, etc.) no number of successful runs tells you there is no bug as much as one unsuccessful run tells you there is a bug.

Quote:
Could this be an issue with not having up-to-date C++ compiler or something of that nature?
Maybe an incorrectly installed C++ compiler (if it really isn't a bug in your code). The compile/link process might make the resulting executable depend on an incompatible version of the C++ library to the one that will be found at run time.

Quote:
I tried just printing a "1" before any variables were even initialized. It wouldn't even do that much before segmentation fault.
Static variables almost anywhere in the code can be initialized before the first executable atement of main, so quite a lot might be happening before that segmentation fault.

The best thing to do with a segmentation fault is use gdb to find out where the segmentation fault occurs.

Last edited by johnsfine; 04-28-2009 at 02:36 PM.
 
Old 04-28-2009, 02:38 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
If you've got static objects, their constructors will get called. Those constructors might be causing your segfault. Try using gdb to find out where it exactly crashed.
 
Old 04-28-2009, 02:46 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

tuxdev and johnsfine are correct. A lot could be happening before your first line of code gets called, and you really should rebuild everything with "-g" and run the program in "gdb" to get a better idea where the problem is.

One additional note: C/C+ both use "buffered I/O". It's entirely possible that your "print 1" actually got called, but never got a chance to display.

WORKAROUND:
Code:
fprintf (stderr, "1\n");
<= Stream "stderr" is unbuffered; "fprintf (stderr)" - unlike "printf" or "cout" - is pretty much guaranteed to print immediately.

'Hope that helps .. PSM

Last edited by paulsm4; 04-28-2009 at 03:46 PM.
 
Old 04-28-2009, 03:02 PM   #5
gmo
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Ah okay, thank you all very much for the replies! I'll look into using gdb.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
Segementation Fault Error (Core Dumped) ??? Mistro116@yahoo.com Programming 8 10-17-2005 12:10 PM
mod_perl causes segementation fault in Apache when php runs john lee Linux - Newbie 1 10-13-2005 01:44 AM
Segementation fault on Multi threaded server on ia-64 Linux Latha Linux - Software 3 09-09-2004 07:40 PM
SDL Segementation Fault when calling function SDL_LockSurface fatherg Programming 2 03-29-2004 04:07 PM

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

All times are GMT -5. The time now is 06:24 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration