LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segementation fault when trying to run a working program (C++) (https://www.linuxquestions.org/questions/programming-9/segementation-fault-when-trying-to-run-a-working-program-c-722293/)

gmo 04-28-2009 01:07 PM

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?

johnsfine 04-28-2009 01:25 PM

Quote:

Originally Posted by gmo (Post 3523798)
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.

tuxdev 04-28-2009 01:38 PM

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.

paulsm4 04-28-2009 01:46 PM

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

gmo 04-28-2009 02:02 PM

Ah okay, thank you all very much for the replies! I'll look into using gdb.


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