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=newint [mk];
in_node[ms];
next_incont= NULL;
}
why dont you take in_node as pointer like you have taken for "key" and allocate memory???
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 ..............
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:
#include <iostream>
usingnamespace 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:
#include <iostream>
usingnamespace 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.
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 :
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..
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.