Hi all, I have some problems with my program, as it always compiles with segmentation fault after it run finishes. ANy help is appreciated
Program received signal SIGSEGV, Segmentation fault.
0x00375d37 in ?? () from /lib/tls/i686/cmov/libc.so.6
(gdb) bt
#0 0x00375d37 in ?? () from /lib/tls/i686/cmov/libc.so.6
#1 0x0804992b in Chord::isValid(char*) ()
#2 0x0804965b in Chord::Read(std::string) ()
#3 0x080491e7 in main ()
and the codes involved
void Chord::Read(string filename) {
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) {
cout << "File not exists" << endl;
exit(1);
}
while (inFile.good()) {
getline(inFile, line); //read instruction line by line
//process instruction, first extract the command
char *inst = strtok(&line[0], " "); //command
int num, num2;
string data;
if (isValid(inst))
{
//process valid instruction
if (strcmp(inst, "insert") == 0)
{
num = atoi(strtok(NULL, " ")); //extract peer number
data = strtok(NULL, "\0");
//cout << data << endl;
}
else
{
num = atoi(strtok(NULL, " ")); //extract peer number
//if (strcmp(inst, "find") == 0)
// num2 = atoi(strtok(NULL, " ")); //extract key
}
} else
continue; //skip invalid instruction
bool Chord::isValid(char *inst) {
if (strcmp(inst, "init") == 0
|| strcmp(inst, "addpeer") == 0
|| strcmp(inst, "removepeer") == 0
|| strcmp(inst, "insert") == 0
|| strcmp(inst, "print") == 0)
return true;
return false;
}