To make it easier to read, only the relevant code is below, and due to circumstances beyond my control im stuck using Xcode for the next week or so (hate this debugger).

I need help fixing a seg_fault in my current program. It occurs the last time "buildPaths" is called. I feel like im going outside of the "thePaths" vector, but the fixes I've tried did nothing.

Please tell me either
A) A solution, where the problem was, how to avoid it, how to go about fixing it
B) Where the problem is, how to go about fixing it, how to avoid it

Remember i basically have no debugger for a while


Sample input
1
3 2
1 2
2 3
Output:
2
1 3


EDIT: If you know of something better than Xcode for a mac please share

struct path {
	int from;
	int to;
	bool good;
};
struct store {
	int numBadPaths;
	bool open;
};
class aMap  {
private:
	int next;
	int numberCities;
	vector <path>thePaths;
	vector <store>theStores;
	bool testMap();	
	void tallyBadPaths ();
	
public:
	void initialize (int numCities, int numPaths);
	void buildPaths (int form , int to);
	void removeStores();
	void print();
	
};
void aMap::buildPaths(int from, int to){
	
	thePaths[next].from = from;
	thePaths[next].to = to;
	thePaths[next].good = false;
	next++;
	
}
void aMap::initialize(int numCities, int numPaths) {
	theStores.resize(numCities);
	thePaths.resize(numPaths);
	numberCities = numCities;
	next = 0;
	for (int i = 0; i < theStores.size(); i++) {
		theStores[i].open = true;
	}
}
int main () {
	int numCases, numCities, numPaths, from, to;
	aMap Boston;
	
	cin >> numCases;
	
	for (int i = 0; i<numCases; i++) {
		cin >> numCities >> numPaths;
		
		Boston.initialize(numCities, numPaths);
		
			for (int j = 0; j<numPaths; j++) {
				cin >> from >> to;
				cout << "Build Paths" << endl;
				Boston.buildPaths(from, to);
			}
		cout << "Remove Stores";
		Boston.removeStores();
		Boston.print();
	}
    return 0;
}

Recommended Answers

All 3 Replies

whats your removeStores function looks like

Ran my program with removeStores never being called, but it didnt work. Here it is anyways

void aMap::removeStores() {
	int gnumBadPaths = 0; //greatest num bad paths
	int storeWMBP; //Store with most bad paths
	
	tallyBadPaths();
	
	if (testMap() == false ) {		//If map doesnt meet requirements delete a store
		for (int i = 0; i < theStores.size(); i++) {
			if (theStores[i].numBadPaths > gnumBadPaths) {
				storeWMBP = i;
			}
		}
		
		theStores[storeWMBP].open = false;
		
		tallyBadPaths();
		
		if (testMap() == false) {	//store closed, still doesnt pass test
			removeStores();
		}
	}
}

I got it working, but i still don't know exactly what the problem was. Here's the code i changed, gonna leave this marked as unsolved until the original problem is at least spotted.

void aMap:: initialize(int numCities, int numPaths) {
	numberCities = numCities;	
	thePaths.resize(numPaths*2);
	next = 0;
}

void aMap::buildPaths (int from, int to) {
	thePaths[next].from = from;
	thePaths[next].to = to;
	thePaths[next].good = false;
	next++;
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.