Hi everyone,

So I have a binary tree class, Stree, that holds names of cities and pointers to other cities. Running the program and typing "insert Memphis Atlanta" inserts a node with Memphis, and inserts a node with Atlanta as Memphis's left child. Typing "insert Memphis Tampa" inserts a node with Tampa as Memphis's right child. Typing "insert" with two cities not already in the tree should generate an error, but when I try it, i get a seg fault.

The insert function runs a recursive function, searchInsert, that finds the city and sets a pointer to that city, target:

void Stree::searchInsert(string city1, bool &found, Node*& target, Node* root) {
	//base case
	if (root != NULL) {
		searchInsert(city1, found, target, root->m_left);
		if (root->city == city1) {
			target = root;
			found = true;
			cout << "found city " << city1 << " at (" << root << ")" << endl;
		}
		searchInsert(city1, found, target, root->m_right);
	}
}

Coming out of the recursion, insert proceeds to insert the new city somewhere:

//if the city has no current destinations
        if (target->m_left == NULL) {
	    target->m_left = new Node(city2);
	    cout << "inserted " << city2 << " at (" << target->m_left << ")" << endl;
	} 
	//if the city has 1 destination
	else if (target->m_right == NULL) {
	    target->m_right = new Node(city2);
	    cout << "inserted " << city2 << " at (" << target->m_right << ")" << endl;
	}
	//if the city already has 2 destinations
	else {
            cerr << "Error: could not insert <" << city1 << ", " << city2 << ">";
            errors = 1;
	}
		
	if (found == false) {
            if (errors == 0) {
                cerr << "Error: could not insert <" << city1 << ", " << city2 << ">";
            }
	}

Somewhere along the above code, when the function cannot find the city or there are 2 destinations already, it segs faults. And i cannot see why it would. Any help would be greatly appreciated.
Thanks!

Recommended Answers

All 6 Replies

You should step through the code with a debugger and determine exactly which line the segfault happens on. This will give you (and us) a better starting point.

You should step through the code with a debugger and determine exactly which line the segfault happens on. This will give you (and us) a better starting point.

Well I would, except I get some weird errors. I use g++, so I go to type in "g++ train", train being the program name, and i get a bunch of mumbo-jumbo:

train: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../crt1.o:(.text+0x0): first defined here
train:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../crt1.o:(.rodata+0x0): first defined here
train: In function `_fini':
/glibc-tmp-b75955b10861dacaae44bdced9fc4b9c/glibc-2.7/build-glibc-2.7/csu/crti.S:41: multiple definition of `_fini'
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../crti.o:/glibc-tmp-b75955b10861dacaae44bdced9fc4b9c/glibc-2.7/build-glibc-2.7/csu/crti.S:41: first defined here
train:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../crt1.o:(.rodata.cst4+0x0): first defined here
train: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../crt1.o:(.data+0x0): first defined here
train: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i486-slackware-linux/4.2.4/crtbegin.o:(.data+0x0): first defined here
train: In function `_init':
/glibc-tmp-b75955b10861dacaae44bdced9fc4b9c/glibc-2.7/build-glibc-2.7/csu/crti.S:15: multiple definition of `_init'
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../crti.o:/glibc-tmp-b75955b10861dacaae44bdced9fc4b9c/glibc-2.7/build-glibc-2.7/csu/crti.S:15: first defined here
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: error in train(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status

If your using GNU's compiler then the compile should be

g++ source.cpp -o executable_name

The errors your receiving indicate that you have two main functions...GNU reduces the main function to _start.

If your using GNU's compiler then the compile should be

g++ source.cpp -o executable_name

The errors your receiving indicate that you have two main functions...GNU reduces the main function to _start.

Well I have something called a Makefile that puts everything together:

train: train.o stree.o
	g++ -Wall -g -o train train.o stree.o

train.o: train.cpp stree.h
	g++ -Wall -g -c train.cpp

stree.o: stree.cpp stree.h
	g++ -Wall -g -c stree.cpp

clean:
	rm -f train train.exe stree.o train.o

and this is supposed to generate the .exe as train.exe. So I just assume all I need to do is type "g++ train".
And as far as having two mains, I just checked, and I'm pretty sure I only have one :/ it compiles okay, and runs okay, which wouldn't make sense if I had two mains. Here is the file with main() in it:

#include <iostream>
#include <string>
#include <stdlib.h>

#include "stree.h"

using namespace std;

int main() {
	string command, city1, city2;
	Stree stree;
	
	while (cin >> command) {
		if (command == "insert")
		{
			cin >> city1;
			cin >> city2;
			stree.insert(city1, city2);
		}
		if (command == "remove")
		{
			cin >> city1;
			stree.remove(city1);
		}
		if (command == "print")
		{
			cin >> city1;
			stree.print(city1);
		}
		if (command == "distance")
		{
			cin >> city1;
			cin >> city2;
			stree.distance(city1, city2);
		}
        }
	return 0;
}

Nope, if you have a Makefile, all you have to do is type 'make' :)

Oh if you have a make file then type Make.

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.