problem with converting object from type to type

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 !!!!!!

bptree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include "bptree.h"
#include "bptree2.h"
using namespace std;
//int level;
#define NULL  0

int ST;
int PREV_ST;
int N;

int x=0;

void bptree::newhead(string* m_data)
{
	cout<<"     iam the head   ";
	internal_container *temp;
temp =new internal_container(max_key, max_slot);
						internal_container *np;
						incont = new internal_container(max_key, max_slot);//the new head
						np = head;   //np the will become the old incont
						head = incont;  //the new inconthead->cur_in_slot = 0;
						//head->cur_key = 0;//by default
						head->level= (np->level+1);
						x=(head->level)-1;
						head->keys_up[0]=NULL;
						head->keys_down[head->cur_key] = (long) np;
						head->cur_key++;
						np->keys_up[0] =  (long) head;
						head->in_node[head->cur_in_slot].st= PREV_ST;
						//head->in_node[head->cur_in_slot].et= np->in_node[incont->cur_in_slot].et;
						head->isleaf = false;
						head->cur_et=0;
						head->cur_in_slot++;

						for(int z=1; z < (head->level); z++)
						{
							cout <<"    z= "<<z<<"		";
												temp=incont;
												incont = new internal_container(max_key, max_slot);//the new head
												incont->keys_up[0]=(long) temp;
												temp->keys_down[temp->cur_key]=(long) incont;
												temp->cur_key++;
												incont->level=x;
												x--;
												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";}

};
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



void bptree::newleafcont()
{

	cout<<"		insert leaf_container	  ";
	leaf_container *xyz;
	xyz=leafcont;
	leafcont = new leaf_container(max_slot);
	incont->keys_down[incont->cur_key] = (long) leafcont;
	leafcont->keys_up[0] =(long) incont;
	leafcont->prev_leafcont=xyz;
	xyz->next_leafcont=leafcont;
//	leafcont->cur_leaf_slot = 0; //by defualt in intilization
	incont->cur_key++;


}
;

void bptree::insert_internal()
{

	incont->in_node[incont->cur_in_slot].st = ST;

	incont->in_node[incont->cur_in_slot].et = NULL;
	incont->cur_in_slot++;
	cout<<"		insert internal  ";
}
;
void bptree::insert_leaf(string *mdata)

   {

		leafcont->leaf_node[leafcont->cur_leaf_slot].st = ST;
		leafcont->leaf_node->creat_mob_list(N);
		for(int x=0;x<N;x++)
		{
		leafcont->leaf_node->mobile_data[x].et=NULL;
		leafcont->leaf_node->mobile_data[x].mob_id=mdata[x];
		cout<<"\n			mob_id    "<<leafcont->leaf_node->mobile_data[x].mob_id<<endl;
		}
		leafcont->cur_leaf_slot++;
	}
;
void bptree::insert(string *m_data)
{
cout<<"\n insert ST    "<<ST<<"    ";//"***********************"<<endl;
	if (head == NULL)
	{

		insert_internal();
		insert_leaf(m_data);
		head = incont;
		incont->keys_down[incont->cur_key] = (long) leafcont;
		incont->keys_up[incont->cur_key]=NULL;
		leafcont->keys_up[0]=(long) incont;
		head->keys_up[0]=NULL;
		incont->level=1;
		incont->cur_key++;

	}//end if
	else//head!=NULL
	{
		if (incont->cur_key <= max_key)
		{
			if ((leafcont->cur_leaf_slot < max_slot)) // insert new LEAF

			{
				cout<<"		insert leaf  	";

				insert_leaf(m_data);
				PREV_ST=ST;
				if((incont->level!=1)&&(leafcont->cur_leaf_slot ==max_slot)&&(incont->cur_in_slot < max_slot))
					insert_internal();
			}//end if(leafcont->cur_leaf_slot<max_slot)

			else if (incont->cur_key < max_key)   //insert new leaf container
			{

				newleafcont();
				insert_leaf(m_data);
				//if (incont->cur_in_slot < max_slot) //because i want to add the last internal with its key
				if((incont->level==1)&&(incont->cur_in_slot < max_slot))
				insert_internal();

					}//end else if
			else if ((leafcont->cur_leaf_slot >= max_slot) && (incont->cur_key== max_key))
			{

			newintcont(m_data);
			}
		}//end if((incont->cur_key<max_key)||(leafcont->cur_leaf_slot<max_slot))



	}//end  head !=NULL

}; //end insert



int main()
	{
	bptree tree(3, 2);
	ifstream fin("data");
	string s;
	string *DATA;
	string subs;
	while (getline(fin,s))
	{
		istringstream iss(s);
		int c=1;

		cout<<"\n****************************\n	reading new line		"<< s <<endl;
		while(iss)
		  {

				iss >> subs;
				if (c==1)
				{
					ST=atoi(subs.c_str());
					c++;
				//	cout<<"  c==1    "<< subs<<endl;
				}

				else if(c==2)
				{
					N=atoi(subs.c_str());
					c++;
					//cout<<" c==2    "<< subs<<endl;
					DATA=new string [N]();
				}
				else
				{
					for(int z=0; z<N; z++)
					{
				//		cout<<"  "<< subs<<endl;
						DATA[z]=subs;
						iss >> subs;
						//cout<<"   "<< subs<<endl;
					}

				}

			}

			tree.insert(DATA);
			delete[] DATA;

	}
	//tree.print();
				cout<<"\n  end  \n";
			return 0;

		}//end main


Last edited on

bptree2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#ifndef B_PLUS_TREE2
#define B_PLUS_TREE2

#include <iostream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include "bptree.h"
using namespace 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=new long [mk]();
			     keys_up=new long [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=new long [1]();
				next_leafcont=NULL;
				prev_leafcont=NULL;
			}

		};//end class leaf_container


//}; ///end class bptree


#endif


bptree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef B_PLUS_TREE
#define B_PLUS_TREE
#include <iostream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include "bptree2.h"
using namespace std;
class bptree
    {
	public:

		int max_key;
		int max_slot;

		internal_container * head;
		internal_container *incont;
		leaf_container *leafcont;
		void insert(string* );// ndata, alldata
		void insert_leaf(string *);// number of mobile nodes
		void insert_internal();
		void newleafcont();
		void print();
		void print_data(internal_container *);
		void print_data(leaf_container *);
	        void newintcont(string* );//mob_id, st
	        void newhead(string* );

		//initilization
		bptree(int mk,int ms)
		    {
			max_key = mk;
			max_slot=ms;
			//cur_leaf_slot=0;
			//cur_key= 0;
			//cur_in_slot=0;
			head=NULL;
			incont = new internal_container(max_key, max_slot);
			leafcont=new leaf_container(max_slot);
		    }
	};//end class bptree


#endif


DATA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1 3 m2 m3 m4
2 1 m5
3 2 m2 m8
4 3 m2 m3 m4
5 1 m5
6 2 m2 m8
7 1 m7
8 2 m8 m8
9 2 m2 m8
10 1 m7
11 2 m8 m8
12 2 m2 m8
13 1 m7
14 2 m8 m8
15 2 m2 m8
16 1 m7
17 2 m8 m8
18 2 m2 m8
19 1 m7




it is terminated in the bolded lines( line 82 in cpp file),, it run true untile reading the 7th data from DATA file (bolded)

any help ,, please !!!!!
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

ahaaaaaaaaaaaaaaaaaaaa ,, i get it ,,
i have to initilie the temp pointer and then let him point to NULL

i will check that
thanks
i initilize the temp pointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.


oh yes,, that is true :D

working with all that pointers make my head like a bump :|

thanks,, it is worked now,, i wil test it with bigger example and i will fix the way of reading the file
can u please help printing the tree ??!!??
i know it is not easy ,, i start like this ,, and i know it is rong !!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void bptree::print()

	{
	internal_container *p;
	p = new internal_container(max_key, max_slot);
		int l;
		p=head;
		cout<<"\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$\n my tree \n$$$$$$$$$$$$$$$$$$$$$$$$\n";
		for(int c=0; c<=(head->cur_key);c++)
		{

		cout<<"\n branch "<<c<<"   "<<head->in_node[c].st;
			for(l=head->level; l<=1  ;  l--)
				{
				for(int x=0; x<=(p->cur_key);x++)
					cout<<"\nlevel  "<<l<<"  "<< p->in_node[x].st;
					p=(internal_container *)p->keys_down[0];

				}//end second for

		}//end for

	}//end print
;


u have to add this code in cpp file ,,, there is prototype in the header file , and from the main call it by
tree.print()


:D
Topic archived. No new replies allowed.