Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segmentation fault. correction needed. Unix

Status
Not open for further replies.

itgeeksingh

Technical User
Dec 7, 2005
1
US
I wrote this program in windows and works fine in windows xp. But now my prof is trying it in UNIX and he is geting segmentation fault in option 2. here is the main.cpp file

#include <cstdlib>
#include <iostream>

#include "interval_tree.h"

using namespace std;

//the menu options...
int show_menu()
{
cout << "\n/**** Options ****/";
cout << "\n1. Create tree";
cout << "\n2. Insert interval";
cout << "\n3. Search interval";
cout << "\n4. Print";
cout << "\n0. Exit";
cout << "\n\nSelect any option: ";

int option;
fflush(stdin);
cin >> option;
return option;
}

//parse a given string in order to build an interval
IntInterval* parse_interval(char* data)
{
char* ptr = strchr(data,',');
int pos=(ptr-data);
if(pos > 0)
{
char* first = strtok (data,",");
char* second = strtok (NULL, ",");
return new IntInterval( atoi(first), atoi(second));
}
return NULL;
}

//draw the tree in the console
void serialize(TreeNode *_tree, int _level)
{
for(int i=0; i<_level; i++)
cout << "--";
_tree->get_interval()->Print();
cout << endl;
if( _tree->get_left()->get_interval() != NULL)
serialize(_tree->get_left(),_level+1);
else
if( _tree->get_right()->get_interval() != NULL)
{
for(int i=0; i<_level+1; i++)
cout << "--";
cout << "L\n";
}
if( _tree->get_right()->get_interval() != NULL)
serialize(_tree->get_right(),_level+1);
else
if( _tree->get_left()->get_interval() != NULL)
{
for(int i=0; i<_level+1; i++)
cout << "--";
cout << "R\n";
}
}

int main(int argc, char *argv[])
{
IntervalTree* _intTree = NULL;
system("color A");
//loop
while(true)
{
int option = show_menu();
switch(option)
{
case 0: //exit
return 0;

case 1: //create tree
{
_intTree = new IntervalTree();
puts("The Tree was created succesfully!\n");
}
break;

case 2: //insert node
if(_intTree != NULL)
{
printf("Type an Interval a,b: ");
fflush(stdin);
char data[10];
gets(data);
//inser the node
_intTree->Insert( parse_interval(data));
}
else
puts("No tree created!\n");
break;

case 3: //serch interval
if(_intTree != NULL)
{
printf("Type an interval a,b to search: ");
fflush(stdin);
char data[10];
gets(data);

Stack<void *> * _intervals = _intTree->IntervalSearch( parse_interval(data));
while(!_intervals->Empty())
{
IntInterval* _int = (IntInterval*)_intervals->Pop();
_int->Print();
printf("--");
}
printf("\n");
}
else
puts("No Tree created!\n");
break;

case 4: //print the tree
if(_intTree != NULL )
if( _intTree->get_root()->get_left()->get_interval() != NULL)
serialize(_intTree->get_root()->get_left(),0);
else
puts("Empty tree!!!");
else
puts("No Tree created!\n");
break;
}

system("pause");
system("cls");
}
}

I am new to unix. i tried to compile main.cpp using g++, it gives me following errors. Somebody could help in correcting code.

g++ main.cpp
main.cpp: In function `class IntInterval * parse_interval(char *)':
main.cpp:28: implicit declaration of function `int strchr(...)'
main.cpp:28: initialization to `char *' from `int' lacks a cast
main.cpp:32: implicit declaration of function `int strtok(...)'
main.cpp:32: initialization to `char *' from `int' lacks a cast
main.cpp:33: initialization to `char *' from `int' lacks a cast
 
1. Use CODE tag for your snippets (see Process TGML link on the form).
2. strchr() and strtok() declared in <cstring> header, add it.
3. Don't mix C++ and C style i/o on cin/cout/stdin/stdout...
 
> fflush(stdin);
> gets(data);
Both are these are big NO-NO's
1. fflush(stdin) is undefined - what happens next (if anything) is anybody's guess.

2. gets() is the worlds most dangerous function - can you say "buffer overflow". Didn't the linker warn you gets() is deprecated?

> char* ptr = strchr(data,',');
> int pos=(ptr-data);
You're assuming success from strchr()

> I wrote this program in windows and works fine in windows xp
Ah, another example of "works for me" != "bug free".

> But now my prof is trying it in UNIX and he is geting segmentation fault in option 2
There's some functionality, which I guess is in your header file, which you don't post.
Anything could be happening in there.

Putting actual code (not just the prototypes) in header files is a bad move.

> _intTree = new IntervalTree();
ALL symbols beginning with an underscore are reserved for the implementation - you should not be using them in your code.
Who knows, you could be picking up some variable from the system rather than your code.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top