hello every body,, i have a small problem in big program,, i will write the hole code, and point to the problem line,, it gives me no error in compilation,, but in execution it terminat !!!!!!
#ifndef B_PLUS_TREE2
#define B_PLUS_TREE2
#include <iostream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include "bptree.h"
usingnamespace std;
class mob
{
public:
int et;
string mob_id;
};//end class mob
class internalslot
{
public:
int st;//start time
int et;//end time
};//end class internal
class internal_container
{
public:
internalslot *in_node;
long *keys_down;
long *keys_up;
bool isleaf;
int level;
//internal_container *next_incont; //i dont need it
int cur_key;
int cur_in_slot;
int cur_et;
internal_container(int mk, int ms)
{
/* for "keys_down" the number of keys have to be equal to max_key
* because there is max_key pointers of children for each container,,
* but
*
* the "key_up" need only one pointer key ,, because the container has only one parent.....
*
*/
in_node= new internalslot [ms]();
keys_down=newlong [mk]();
keys_up=newlong [1]();
cur_key=0;
cur_in_slot=0;
cur_et=0;
level=0;
//next_incont= NULL;
isleaf=true;// is the key point to lesfs or index,, by default it point to leaf node
}
};//end class internal_container
class leafslot
{
public:
int st;
mob *mobile_data;
int n_data;//number of mobile nodes
void creat_mob_list(int n)
{
mobile_data=new mob[n];
}
///leafslot *nextleaf; // no pointer,, becUSE It is record [][][]
};//end class leaf
class leaf_container
{
public:
leafslot *leaf_node;
int cur_leaf_slot;
long *keys_up;
leaf_container *next_leafcont;
leaf_container *prev_leafcont;
leaf_container( int ms)
{
leaf_node=new leafslot[ms]();
cur_leaf_slot=0;
cout<<"0";
keys_up=newlong [1]();
next_leafcont=NULL;
prev_leafcont=NULL;
}
};//end class leaf_container
//}; ///end class bptree
#endif
There are problem all around the area Lines 81, 82, 83, 84 etc.. and they all involve the temp pointer (note I include line 81 as well). temp=(internal_container *)incont->keys_up[0];
It seems that keys_up[0] value is always 0 - which makes temp 0, that is to say that temp is a null pointer.
but why it is terminated,,
it is not rong that the value of ( incont->keys_up[0] =null ), because it is the default value , which gives me the temp is a pointer for null
when insert the 7th line it have to go to line 127 and not enter the while,, ,, because the while will not be true and go to line 127
but it is terminated !!!!!!!!!!!
BUT you actually try to use (dereference) the NULL pointer - on lines 83 and 84
1 2
cout<<"\n\n temp->keys_up[0] "<<temp->keys_up[0]<<endl; // error - attempting to use a null pointer
while ((inter==false)&& (temp->keys_up[0]!=NULL ))//iam the head - ERROR attempting to use a null pointer
Also on line 81 - the second statement on this line is trying to use an unitialised pointer cout<<"\n\n temp->keys_up[0] \n"<<" " << incont->keys_up[0]<<"\n ";cout<<temp->keys_up[0]<<endl; //error - temp is not yet initialised
void bptree::newintcont(string* m_data)
{
internal_container *temp;
temp =new internal_container(max_key, max_slot);bool inter=false;
cout<<"\n ****insert newintainercontainer****** \n";
//iam not the head
cout<<" iam not the head \n";
cout<<"\n\n temp->keys_up[0] \n"<<" " << incont->keys_up[0]<<"\n ";cout<<temp->keys_up[0]<<endl;
temp=(internal_container *)incont->keys_up[0];
cout<<"\n\n temp->keys_up[0] "<<temp->keys_up[0]<<endl;
while ((inter==false)&& (temp->keys_up[0]!=NULL ))//iam the head
{
if(temp->cur_key<max_key) // ihave empty slot in my parent
{
cout<<"\n inter==false && temp->cur_key<=max_key \n";
for(int z=1; z < (temp->level); z++)
{
cout <<" z= "<<z<<" ";
incont = new internal_container(max_key, max_slot);
//temp=incont;
incont->keys_up[0]=(long) temp;
temp->keys_down[temp->cur_key]=(long) incont;
incont->level=temp->level -1 ; // temp is the parent ,, the newincont level is less than the parent level by 1
temp->cur_key++;
if(incont->level!=1)
{incont->isleaf=false;cout<<"notleaf";}
//set st and et
//incont->cur_key++;
}//end for
if(incont->level==1)
{
newleafcont();
insert_internal();
insert_leaf(m_data);
//incont->keys_down[incont->cur_key] = (long) leafcont;//set in newleafcont() function
//leafcont->keys_up[0]=(long) incont;// same of up_line
//inserting leafs and internal data at the last intenar_container created
}//end if
else
{cout<<"\n\nERROR\n\n";}
inter=true;
}//end if
//insert leafcontainer
else
{
temp=(internal_container *)temp->keys_up[0];
cout<<"\n temp=(internal_container *)temp->keys_up[0] \n";
}
}//end while
if(inter==false)
{
newhead(m_data);
}
};//newintcontainer
but it is code still terminated !!!!!!!!!!!!!
where is the problem ?? it is regular that the pointer point to NULL value !!!! it a tree,, and the (incont->keys_up[0]) is a pointer for the parent , and the root has no parent , so its point to NULL ..............
is there problem with NULL value ?? should i put it 0 ?? but it is NULL not zero ,,,,,,,,, i know that the NULL is the same with 0 !!!!!!!!!
You are missing the point.
If a pointer has the value of 0 then you cannot dereference it - your program will crash.
Ay you say - the pointer will always be 0 - so you cannot do those cout statements and you cannot use it the way you are doing in the while statement.
So what we will do - we will get rid of those cout statements - and in the while statement
we will test the actual value of the pointer itself.
1 2 3 4
cout<<"\n\n temp->keys_up[0] \n"<<" " << incont->keys_up[0]<<"\n ";// got rid of that second cout statement
temp=(internal_container *)incont->keys_up[0];
//got rid of that cout statement that was here
while ((inter==false)&& (temp != NULL ))//iam the head - Note: we now test the value of the pointer itself
That should now go all th way through.
But your program will crash after reading the file because you are not reading the data file correctly (if your file has empty lines at the end your current code will try reading these and will crash - so for now make sure your data file does not have empty lines at the end .)
We will correct this problem later.