C++ Programming
04-09-2009, 11:00 AM
i wrote the code below for a C++ programming class and i get a segmentation fault within this part of the code. now i asked my teacher and he told me that there's just a line in the wrong place but for the life of me i can't figure out which line is out of place. i thought i'd turn to some people with more experience with looking at code and finding the errors. it's really a simple problem i have i'm just not seeing it though.. please help me!
p.s. sorry if i posted the code the wrong way for the forum
void Search::Store(char *k, char *d) {
struct TBox *p1,*p2;
int i;
p1 = new TBox;
if (Start==NULL) {
p1->left = NULL;
p1->right = NULL;
p1->key = new char[strlen(k)+1];
p1->data = new char[strlen(d)+1];
strcpy(p1->key,k);
strcpy(p1->data,d);
Start=p1;
return;
p1=Start;
while(p1!=NULL) {
if ( (i=strcmp(k,p1->key)) == 0 ) {
delete [] p1->data;
p1->data = new char[strlen(d)+1];
strcpy(p1->data,d);
return;
}
if ( i < 0 ) {
p2=p1;
p1=p1->left;
continue;
}
p2=p1;
p1=p1->right;
}
}
p1 = new TBox;
p1->left = NULL;
p1->right = NULL;
p1->key = new char[strlen(k)+1];
p1->data = new char[strlen(d)+1];
strcpy(p1->key,k);
strcpy(p1->data,d);
Start=p1;
if (ileft=p1;
else p2->right=p1;
return;
}
p.s. sorry if i posted the code the wrong way for the forum
void Search::Store(char *k, char *d) {
struct TBox *p1,*p2;
int i;
p1 = new TBox;
if (Start==NULL) {
p1->left = NULL;
p1->right = NULL;
p1->key = new char[strlen(k)+1];
p1->data = new char[strlen(d)+1];
strcpy(p1->key,k);
strcpy(p1->data,d);
Start=p1;
return;
p1=Start;
while(p1!=NULL) {
if ( (i=strcmp(k,p1->key)) == 0 ) {
delete [] p1->data;
p1->data = new char[strlen(d)+1];
strcpy(p1->data,d);
return;
}
if ( i < 0 ) {
p2=p1;
p1=p1->left;
continue;
}
p2=p1;
p1=p1->right;
}
}
p1 = new TBox;
p1->left = NULL;
p1->right = NULL;
p1->key = new char[strlen(k)+1];
p1->data = new char[strlen(d)+1];
strcpy(p1->key,k);
strcpy(p1->data,d);
Start=p1;
if (ileft=p1;
else p2->right=p1;
return;
}