Free Advertising | Mobile Phones | Credit Cards | Mobile Phones | Mortgages
Segmentation Fault [Archive] - PCMech Forums

PDA

View Full Version : Segmentation Fault


GSXdan
04-09-2003, 03:19 PM
I am writing a program for my C++ class, and i got the program to compile, but when i run it it outputs a few lines then says "Segmentation Fault", and exits. What could cause this and how would I fix this? The project is due tonight at 11EST, and this is bugging me. I will post the code in a little bit, just wanted to know if there is somewhat of a quick fix or if I just did something stupid.

thanks ^dan

doctorgonzo
04-09-2003, 03:25 PM
What does the program do? Could you post the code?

GSXdan
04-09-2003, 03:34 PM
The program is supposed to keep tract of a number of taxis and keep two arrays, one for taxis in service, and one for taxis needing repair. The taxi can be in both. It reads in info from a data file with commands on what to do. That is the general jist of the program, I am sure by looking at the code you can tell whats supposed to be going on.

thanks ^dan

doctorgonzo
04-09-2003, 03:46 PM
I got it to compile properly and run without an error, so it may be your compiler. What compiler are you using?

GSXdan
04-09-2003, 03:48 PM
not exactly sure what compiler it is, but it is being run on my college's unix server. What did you use to compile it?

thanks ^dan

doctorgonzo
04-09-2003, 03:53 PM
I'm using the GNU complier, g++. When you compile your program, how do you do it? What commands do you use?

GSXdan
04-09-2003, 03:54 PM
Yea that is what I use on the unix server, i just type g++ lab5.cpp

GSXdan
04-09-2003, 03:56 PM
can you post the output that you get when you run it?

thanks ^dan

doctorgonzo
04-09-2003, 03:58 PM
At what point does it crash for you?

This is what I get:

0
11111
74532

Here are the taxis in use:
Cab number 64988 is in use.
Cab number 64988 needs to be repaired

Cab number 11395 is in use.
Cab number 11395 needs to be repaired
0
11111
64988
74532

Here are the taxis in use:
0
30412

Here are the taxis needing repair:
Cab number 30412 is in use.
Cab number 30412 needs to be repaired

0
11111
11395
30412

Here are the taxis needing repair:
0
11111
11395
64988
74532

Here are the taxis in use:
0
0
11111
11395
12146

Here are the taxis needing repair:
Cab number 64988 is in use.
Cab number 64988 needs to be repaired

Cab number 99999 is in use.
Cab number 99999 needs to be repaired

Taxi not found.

Sorry, taxi already in array.

0
11111
11395
22233
48620
64988
74532

Here are the taxis in use:
0
0
0
11111

Here are the taxis needing repair:
Cab number 87664 is in use.
Cab number 87664 needs to be repaired

0
11111
11395
22233
26777
30412
48620
54112
64988
74532
87664

Here are the taxis in use:
Sorry, taxi already in array.

Sorry, taxi already in array.

0
0
11395
21163
22233
26777
30412
48620
54112
64988
74532

Here are the taxis in use:
0
0
0
11111

Here are the taxis needing repair:
0
0
0
11111

Here are the taxis needing repair:

GSXdan
04-09-2003, 04:03 PM
I get this much:
0
11111
74532

Here are the taxis in use:
Cab number 64988 is in use.
Cab number 64988 needs to be repaired
Segmentation Fault



That is still not the right output though. I have what the output should be. I think the only thing that should not be there is the ID numbers w/o any kind of header, like this part:

0
11111
74532

GSXdan
04-09-2003, 04:05 PM
I checked the code and there are no cout statements outputting what ID numbers are read in, so there is some error that is causing that. And i dont know what all those 0's are for.

^dan

GSXdan
04-09-2003, 04:10 PM
Ok, i got rid of the segmentation fault. I forgot to put in code to make sure the array doesnt get more than 10 values, but the output is not correct. getting closer....

void Add(int CabArray[10], int& NumInArr, int ID)
{

int check;


check = Search(CabArray, ID, NumInArr);

if(check == -1){
if(NumInArr > 10){
CabArray[NumInArr+1] = ID;
NumInArr++;
}
}
else
cout << "Sorry, taxi already in array." << endl << endl;

Sort(CabArray, NumInArr);

return;
}

thanks ^dan

GSXdan
04-09-2003, 04:18 PM
This is my first program passing arrays, am i doing that correctly? I have a feeling it is something with the arrays or the number of items in the array variables, since they get passed around so much.

^dan

doctorgonzo
04-09-2003, 04:37 PM
That line you added should be If(NumInArr<10), not greater than.

Arrays in C++ are zero-based, so the Add function shouldn't add one to the index number when adding something to the array. It should start at zero.

Still working...

GSXdan
04-09-2003, 04:46 PM
I make dumb mistakes when i rush, good thing you caught that:)

doctorgonzo
04-09-2003, 05:05 PM
This is what I get now after making all the changes:

Here are the taxis in use:
11111
64988
74532

Cab number 64988 is in use.

Cab number 11395 is in use.
Cab number 11395 needs to be repaired.

Here are the taxis in use:
11111
11395
64988
74532

Here are the taxis needing repair:
11395
30412

Cab number 30412 needs to be repaired.

Here are the taxis needing repair:
11111
11395
22233
30412

Here are the taxis in use:
11111
11395
48620
64988
74532

Here are the taxis needing repair:
11111
11395
12146
30412
56565

Cab number 64988 is in use.

The taxi was not found.

Sorry, can't add.

Here are the taxis in use:
11111
11395
22233
30412
48620
64988
74532

Here are the taxis needing repair:
11111
12146
56565

Cab number 87664 is in use.

Here are the taxis in use:
11111
11395
22233
26777
30412
48620
54112
64988
74532
87664

Sorry, can't add.

Here are the taxis in use:
11111
11395
21163
22233
26777
30412
48620
54112
64988
74532

Here are the taxis needing repair:
11111
12146
56565

Here are the taxis needing repair:
11111
12146
56565

GSXdan
04-09-2003, 05:12 PM
can you post the updated code? the only thing wrong with the output that i can get from the data file is the two repair arrays output at the end. From what i can tell, there should be only one, but that should be good enough. I dont know how many times i have said this, but it is very well deserved; Thank You


^dan

doctorgonzo
04-09-2003, 05:12 PM
Attached is the source file. I used the #define TEST and the #ifdef...#endif blocks to help debug by printing out the arrays every time something was added or deleted. To turn it back on, uncomment the #define TEST line.

Let me know if the output is what it should be.

doctorgonzo
04-09-2003, 05:22 PM
This source takes care of the repeat of the last request for the repair array. You have to test for end of file after you read the line, so I moved the test below the read. Now there is no repeat.

hobey19
04-09-2003, 09:22 PM
doctor gonzo, i am working on the same project with dan, how did u get rid of the zeroes that it was outputting that it is not supposed to. we can't figger that out. dan got it, but i cannot tell how he did it. thanks. hopefully you remember how you did that.

craig

doctorgonzo
04-10-2003, 09:12 AM
The problem was with the sort. When you removed an item from the array and replace it with zero, when you ran the sort to re-order the IDs from lowest to highest, that zero you just entered would be moved to the first item in the array, because it is lowest.

I fixed that by replacing the item you want to remove with a large number, not zero. Then you run the sort, which puts the number at the end. At that point, after you reset the number of elements in the array, you can set that large number to zero. It is all in the Remove function in the code I did.