LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Program, Segmentation Fault (http://www.linuxquestions.org/questions/programming-9/c-program-segmentation-fault-390272/)

Fireball7 12-07-2005 04:05 PM

C++ Program, Segmentation Fault
 
My c++ program keeps giving me a segmentation fault error. I believe the error is in the way i call my main function.

./program.exe student.data name Nelson

is how i call it and

int main(int argc, char *argv[])
{
//program code
}

is the definition in main. Note that the first line in main is a cout statement which does not execute, meaning that the error is somewhere before it.

The program is a C++ program that takes in values (above) and returns them to a CGI script. Thanks.

Josh

Mara 12-07-2005 04:12 PM

There's nothing wrong with the way you run the program. It may be something wrong (and probably is if it's the first line) with the cout. Remember that if cout crashes, it doesn't print the text.

How does the cout line look like?

graemef 12-07-2005 04:15 PM

A segmentation fault is cause by referencing a pointer that has not been properly set. I'm sure that you are getting into your program. The cout may not display becasue you've not put a new line in the cout. Probably what is happing is that the output is sent to cout but it is sitting there waiting for a new line before it displays, then along comes your segmentation fault and everything goes pear shaped :(

Fist make sure that you have requested a new line. YOu can do this is follows:

Code:

std::cout << "Hello \n";
For us to really help you with the segmentation fault we need a little more to go on ;)


graeme.

Fireball7 12-07-2005 04:16 PM

the line looks like

cout << "Got here." << endl;

Fireball7 12-07-2005 04:16 PM

the line looks like

cout << "Got here." << endl;

it is one of the lines i used to find where the segmentation fault was.

(sorry firefox messed up and i posted twice by accident)

graemef 12-07-2005 04:20 PM

I know these are going to sound like a silly questions but let's get them out of the way first.
  • Did you re compile
  • Did it work
  • Did you link
  • Did it work
  • Are you sure? Check the time you program.exe was created

Fireball7 12-07-2005 04:22 PM

whoops
 
Nevermind, i forgot to recompile after I modified my code last time, after I added some functions. My (very stupid) mistake. Thanks to everyone for your help.


All times are GMT -5. The time now is 10:13 PM.