segmentation error with pointers

the header file:

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
#ifndef B_PLUS_TREE

#define B_PLUS_TREE
#include <iostream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

class bptree
{
	public:
		
		int max_key;
		int max_slot;
		int cur_key;
		int cur_leaf_slot;
		int cur_in_slot;
		bptree * head;
		//initilization
		bptree(int mk,int ms)
		    {
			max_key = mk;
			max_slot=ms;
			cur_leaf_slot=0;
			cur_in_slot=0;
			head=NULL;

		    }


		void insert(string mob_id, int st, int et)
		   {
			if(head==NULL)
			{

internal_container incont(max_key, max_slot);
leaf_container leafcont(max_slot);
leafcont.leaf_node[cur_leaf_slot].st=st;
incont.in_node[cur_in_slot].st=st;
incont.keys[cur_in_slot] = (int )&leafcont;
head=( bptree *)&incont;


cout<<"head "<<head<<"  incount  "<< & incont<<endl;
cout<<"int "<<incont.in_node[cur_in_slot].st<<endl<<"  leaf  "<< leafcont.leaf_node[cur_leaf_slot].st<<endl<<"  key  "<<incont.keys[cur_in_slot]<<endl;
cur_leaf_slot++;
cur_in_slot++;
			
	}//end if
else//head!=NULL
 {}//end else
    }//end insert


	class internalslot
		{
		  public:
			int st;//start time
			int et;//end time
			
		};//end class internal 
	class internal_container
		{
		  public:


			internalslot in_node[];
			int *keys;
			internal_container *next_incont;
			internal_container(int mk, int ms)
				{
			     keys=new int [mk];
				in_node[ms];
				next_incont= NULL;
				 }

		};//end class internal_container
	
	class leafslot
		{
		  public:
			int st;
			int et;
			string mob_id;

		};//end class leaf

	class leaf_container
		{
		  public:
			leafslot leaf_node[];
			leaf_container *next_leafcont;
			leaf_container( int ms) {leaf_node[ms]; next_leafcont=NULL;}

		};//end class leaf_container

 
}; ///end class bptree	


#endif 


the main file:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <ostream>
#include "bptree.h"
using namespace std;
int main()          
{
bptree tree(4,3);
tree.insert("m4", 1, 5);
return 0; 
}///end main
           




the problem is that no compilation errors, but when i want to run the file it gives me "segmentation error " !!!!!!!!
i dont know why????!!!!!

i think there is problem with the bolded text........ please help
i use ubuntu os, gcc downloaded
Last edited on
line 39 is wrong:
leafcont.leaf_node[cur_leaf_slot].st=st;
due to this it will overwrite the address of incont.key and which will fault.
incont.keys[cur_in_slot] = (int )&leafcont;

instead of doing:
1
2
3
4
5
6
7
8
9
internalslot in_node[];
			int *keys;
			internal_container *next_incont;
			internal_container(int mk, int ms)
				{
			     keys=new int [mk];
				in_node[ms];
				next_incont= NULL;
				 }


why dont you take in_node as pointer like you have taken for "key" and allocate memory???

thanx for reply........

i want to make an array of class(leafslot) , as line 89 ,, and then change the (st) value :
1
2
3
4
5
6
class leaf_container
		{
		  public:
			leafslot leaf_node[];
..............................
..............................

 
leafcont.leaf_node[cur_leaf_slot].st=st;


and i want to create a pointer for each (array of leafslot)

why i have to use (in_node) as pointer???? it have to be an array of class internalslot ????
the internalslot and keys are in the same container (internal container) ,, and leafslots have to be in another container ..............
Last edited on
do u notice that i make all classes inside the bptree class ......
is that rong ????
no one can help me ????????!!!!!!!!!!!!!???????
hmmm...

but you are taking the wrong approach...
the lines has no meaning:

1
2
3
4
leafslot leaf_node[];
...
...
leaf_container( int ms) {leaf_node[ms]; next_leafcont=NULL;}


you are declaring the array leaf_node with no size and then trying to subscript it: leaf_node[ms]
this will surely fault. to make you understand the point compile this code:

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
#include <iostream>
using namespace std;

class leafslot
{
        public:
        int st;
        int et;
        string mob_id;

};//end class leaf

class leaf_container
{
        public:
        leafslot leaf_node[];
        leaf_container *next_leafcont;
        leaf_container( int ms) {leaf_node[ms]; next_leafcont=NULL;}

};//end class leaf_container

int main()
{
        int max_slot = 1;
        leafslot ls;
        leaf_container leafcont(max_slot);
        leafcont.leaf_node[0] = ls;

        return 0;
}


debug it and it will fault at line: leafcont.leaf_node[0] = ls;
why??? because leafcont.leaf_node dont have any size. now change the program to this and run:

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
#include <iostream>
using namespace std;

class leafslot
{
        public:
        int st;
        int et;
        string mob_id;

};//end class leaf

class leaf_container
{
        public:
        leafslot leaf_node[10];
        leaf_container *next_leafcont;
        leaf_container( int ms) {leaf_node[ms]; next_leafcont=NULL;}

};//end class leaf_container

int main()
{
        int max_slot = 1;
        leafslot ls;
        leaf_container leafcont(max_slot);
        leafcont.leaf_node[0] = ls;
        leafcont.leaf_node[9] = ls;

        return 0;
}


now even if you write at 9th index it wont fault because now its size is 10.
so either make it a pointer or if you want to take an array then set its size.

you cant do this: leafslot leaf_node[];

your code has many instance of this thing.
i think i got ur point, but:

line 16 in ur code, i gave an array size,
leafslot leaf_node[10];

then i resize the array in line 18 ,, with the new size,,
leaf_container( int ms) {leaf_node[ms];}
is that ok ,, to resize the array ??!!??

another question,,
1
2
3
4
5
 leafslot ls;
        leaf_container leafcont(max_slot);
        leafcont.leaf_node[0] = ls;
        leafcont.leaf_node[9] = ls;


i think i can replace it with

1
2
3
4
 
        leaf_container leafcont(max_slot);
        leafcont.leaf_node[0],st = 1;
     


without
1
2
 leafslot ls;
        
thank u for helping me,,,, i do like u suggest,, the segmentation error gone ,,,, but it not give me the right value......... this is the header file after modification,, the modification in bold :

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
#ifndef B_PLUS_TREE

#define B_PLUS_TREE


#include <iostream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
//template <class p>
class bptree
{
	public:
		
		int max_key;
		int max_slot;
		int cur_key;
		int cur_leaf_slot;
		int cur_in_slot;
		bptree * head;
		//initilization
		bptree(int mk,int ms)
		    {
			max_key = mk;
			max_slot=ms;
			cur_leaf_slot=0;
			cur_in_slot=0;
			head=NULL;

		    }


		void insert(string mob_id, int st, int et)
		   {
			if(head==NULL)
			{

internal_container incont(max_key, max_slot);
leaf_container leafcont(max_slot);
leafcont.leaf_node[cur_leaf_slot].st=st;
incont.in_node[cur_in_slot].st=st;
incont.keys[cur_in_slot] = (int )&leafcont;
head=( bptree *)&incont;


cout<<"head "<<head<<"  incount  "<< & incont<<endl;
cout<<"int "<<incont.in_node[cur_in_slot].st<<"  leaf  "<< leafcont.leaf_node[cur_leaf_slot].st<<"  key  "<<incont.keys[cur_in_slot]<<"      "<<&leafcont<<endl;
cur_leaf_slot++;
cur_in_slot++;
			
	}//end if
else//head!=NULL
 {}//end else
    }//end insert

//}//end class bptree
	class internalslot
		{
		  public:
			int st;//start time
			int et;//end time
			
		};//end class internal 
	class internal_container
		{
		  public:

			internalslot *in_node;
			int *keys;
			internal_container *next_incont;
			internal_container(int mk, int ms)
				{
			     keys=new int [mk];
				in_node= new internalslot [ms];
				next_incont= NULL;
				 }

		};//end class internal_container
	
	class leafslot
		{
		  public:
			int st;
			int et;
			string mob_id;

		};//end class leaf

	class leaf_container
		{
		  public:
			leafslot *leaf_node;
			leaf_container *next_leafcont;
			leaf_container( int ms)
		        {
				leaf_node=new leafslot[ms]; 
				next_leafcont=NULL;
			}

		};//end class leaf_container

 
}; ///end class bptree	


#endif



line(44) don't give me the right address !!!

 
incont.keys[cur_in_slot] = (int )&leafcont;


 
cout<<"int "<<incont.in_node[cur_in_slot].st<<"  leaf  "<< leafcont.leaf_node[cur_leaf_slot].st<<"  key  "<<incont.keys[cur_in_slot]<<"      "<<&leafcont<<endl;



thanx in advance :)
Last edited on
rong address !!!!!!!!!!!!!!!!!!!!!!!!!! why ??????????????????

line(44) don't give me the right address !!!
1
2
3
incont.keys[cur_in_slot] = (int )&leafcont;

cout<<"int "<<incont.in_node[cur_in_slot].st<<" leaf "<< leafcont.leaf_node[cur_leaf_slot].st<<" key "<<incont.keys[cur_in_slot]<<" "<<&leafcont<<endl;
then i resize the array in line 18 ,, with the new size,,
leaf_container( int ms) {leaf_node[ms];}
is that ok ,, to resize the array ??!!??


you cant resize your array. to resize you need to take pointer like this:
1
2
3
4
5
char *ptr = (char*)malloz(10 * sizeof(char)); //size is 10 bytes
..
..some code..
//resize here
*ptr = //use realloc to resize to some other length 


leafcont.leaf_node[0],st = 1;
what are you trying to do here?? this will give error!!! leafcont.leaf_node[0] and st are seperated by a comma, what does this mean??



Great!!! the code looks fine now, but dont forget to delete[] the memory you allocated with new[] in your destructors otherwise there will be memory leak.

to resize using new you may use placement_new, i actually forget its use but if im right you are searching for
placement_new

to resize the string/pointers you have taken in your code. read it a little if you need resizeing..
now lets look at your wrong output..
 
what are you trying to do here?? this will give error!!! leafcont.leaf_node[0] and st are seperated by a comma, what does this mean??


it is not (comma) !! it is (dot)

i want to change the st value
 
leafcont.leaf_node[0].st =1
yes ,,, i htink the code is good now,,, but why the address not correct ??????
oh...ok.. you can do anthing now.. but this will just assign a new value but wont resize the array. arrays once declared cannot be resized.

ok.. so you want to store the address by using this:

1
2
incont.keys[cur_in_slot] = (int )&leafcont;
cout<<"int "<<incont.in_node[cur_in_slot].st<<" leaf "<< leafcont.leaf_node[cur_leaf_slot].st<<" key "<<incont.keys[cur_in_slot]<<" "<<&leafcont<<endl;


or you want to use the object later by de-referencing it? see to store the addres you have to take "key" as long and not int so do this:
incont.keys[cur_in_slot] = (long )&leafcont;
and declare this way:
long *keys;

to see the addres do this:
printf("%x\n",incont.keys[cur_in_slot]);
you will see the correct value.


thank u so much.... the problem is solved :)
i conver every address to long ,,, it is work now,, all the addresses are correct

thanks again :-)
Topic archived. No new replies allowed.