473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SEGFAULTS cant figure it out

trying to create a tree of hash nodes, with each node having a max of
5 objects, and a maximum of 5 child nodes...we insert 6 sucesfully and
"explode" to create 3 child nodes, but when it gets to 7 our temp node
has null pointers???
DRIVER PROGRAM::
#include <iostream>
#include "MLH.h"
using std::cout;
using std::endl;

int main()
{
MLH< int tree;

for(int i = 1; i<=300; i++)
{
tree.MLH_insert(i, i );

}

tree.print();
}



MLH.H (data structure)::

#include <iostream>
using std::cout;
using std::endl;
#define Max_levels 8
#include "node.h"
#include "ML_hash.h"

template< typename T >
class MLH
{
friend class Node< T >;
public:
MLH();
int MLH_insert(int, const T &data);
int MLH_delete(int);
int MLH_get(int);
int explode(Node< T *current, Object< T *newObj);
int collapse(Node< T *curr, int);
void print_rec(Node< T *temporary);
void print();
private:
Node< T Sentinel;
Node< T *temp;
int Dlevel;
Node< T nodecount[Max_levels];
int counter;
};

template< typename T >
MLH< T >::MLH()
{
// for (int i = 0; i < Max_levels; i++)
// nodecount[i] = 0;
counter = 0;
Dlevel = 0;

}

template< typename T >
int MLH< T >::MLH_insert(int id, const T &data)
{

temp = &Sentinel;
Node< T *countpath;
int nextnode;

while(1)
{
if (temp->count num_ob) //checks if the node is a stem node
{

nextnode = ML_hash(temp->level + 1, id);
if (temp->nArray[nextnode]==NULL)
{
Node< T *newNode = new Node< T >();
Object< T *newObj = new Object< T >(id, data);

temp->nArray[nextnode] = newNode;
newNode->up = temp;
newNode->level= temp->level +1;
counter++;
if (newNode->level Dlevel)
Dlevel = newNode->level;

temp = newNode;

int r = 0;
while (temp->objectarray[r] != NULL && r<5)
{
r++;
}
temp->objectarray[r] = newObj;

cout<< "You have successfully inserted the
object! Noobsicle!" << endl;
counter++;
countpath = temp;
while (countpath->up != NULL)
{
countpath->count++;
counter++;
countpath = countpath->up;
}
Sentinel.count++;
return 1;
}
else{
temp= temp->nArray[nextnode];

counter++;

// continue;
}
}

if (temp->count < num_ob) /*checks if node has enough space in it*/
{
for(int i = 0; i < num_ob; i++)
{
if (temp->objectarray[i] != NULL)
{
if (temp->objectarray[i]->key == id)
{
cout << "Object is already in list! Noobsicle!" << endl;
counter++;
return 0;

}

}
}
for(int h = 0; h < num_ob; h++)
{
if(temp->objectarray[h] == NULL)
{

Object< T *newObj = new Object< T >(id, data);
temp->objectarray[h] = newObj;

cout<< "You have successfully inserted the object! Noobsicle!" <<
endl;
counter++;
countpath = temp;
while (countpath->up != NULL)
{
countpath->count++;
counter++;
countpath = countpath->up;
}
Sentinel.count++;
return 1;
}

}
break;
}

if (temp->count == num_ob)
{
//first need to check to see if the element is already there
for(int j = 0; j < num_ob; j++)
{
if (temp->objectarray[j]->key == id)
{
cout << "Object is already in list! Noobsicle!"
<< endl;
counter++;
return 0;
}

}
Object< T *newObj = new Object< T >(id, data);
explode(temp, newObj);//moves the current objects in that node into
another node below it
//then it inserts the object we wish to insert
return 1;
}
}
}

template< typename T >
int MLH< T >::MLH_delete(int id)
{
temp = &Sentinel;
int nextnode;
Node< T *countpath;
int noob;

while(1)
{
if (temp->count num_ob)
{
nextnode = ML_hash(temp->level + 1, id);
temp = temp->nArray[nextnode];
counter++;
continue;
}

if (temp->count < num_ob)
{
for(int i = 0; i < num_ob; i++)
{
if (temp->objectarray[i] != NULL)
{
if (temp->objectarray[i]->key == id)
{
// temp->count--;
countpath = temp;
// noob = i;
while (countpath->up != NULL)
{
countpath->count--;
countpath = countpath->up;
counter++;
}
Sentinel.count--;

delete temp->objectarray[i];

while(temp->objectarray[i+1] != NULL && (i+1) < num_ob ){

temp->objectarray[i] = temp->objectarray[i+1];
i++;
}
temp->objectarray[i] = NULL;
cout << "Object was Deleted! Noobsicle!" << endl;
collapse(temp, nextnode);
counter++;
return 1;

}

}
}
for(int h = 0; h < num_ob; h++)
{
if(temp->objectarray[h] == NULL)
{

counter++;
cout<< "The object you are looking for was not found! Noobsicle!"
<< endl;
return 0;
}

}
break;
}

if (temp->count == num_ob)
{
for(int j = 0; j < num_ob; j++)
{
if (temp->objectarray[j]->key == id)
{
// temp->count--;
countpath = temp;
// noob = j;

while (countpath->up != NULL)
{
countpath->count--;
counter++;
countpath = countpath->up;
}
Sentinel.count--;

delete temp->objectarray[j];

while(temp->objectarray[j+1] != NULL && (j+1) < num_ob){
temp->objectarray[j] = temp-
>objectarray[j+1];
j++;
}
temp->objectarray[j] = NULL;
counter++;
cout << "Object was Deleted! Noobsicle!" <<
endl;
collapse(temp, nextnode);
//run collapse() or should i make check in here?
return 1;
}

}
}
}


}

template< typename T >
int MLH< T >::MLH_get(int id)
{

temp = &Sentinel;
int nextnode;

while(1)
{
if (temp->count num_ob)
{
nextnode = ML_hash(temp->level + 1, id);
counter++;
temp = temp->nArray[nextnode];
continue;
}

if (temp->count < num_ob)
{
for(int i = 0; i < num_ob; i++)
{
if (temp->objectarray[i] != NULL)
{
if (temp->objectarray[i]->key == id)
{
counter++;
cout << "Object was found! Noobsicle!" << endl;
return 1;

}

}
}
for(int h = 0; h < num_ob; h++)
{
if(temp->objectarray[h] == NULL)
{

counter++;
cout<< "The object you are looking for is was not found!
Noobsicle!" << endl;
return 0;
}

}
break;
}

if (temp->count == num_ob)
{
for(int j = 0; j < num_ob; j++)
{
if (temp->objectarray[j]->key == id)
{
cout << "Object was found Noobsicle!" <<
endl;
counter++;
return 1;
}

}
}
}


}

template< typename T >
int MLH< T >::explode(Node< T *current, Object< T *newObj)
{
int hashpath;
int ki;
int x=0;
Node< T *countpath;

while(x != 1){

for (int k = 0; k < num_ob; k++)
{
ki = current->objectarray[k]->key;
hashpath = ML_hash(current->level+1 , ki);
if (current->nArray[hashpath]==NULL){
Node< T *newNode = new Node< T >();
current->nArray[hashpath] = newNode;
newNode->up = current;
newNode->level= current->level +1;
counter++;
if (newNode->level Dlevel)
Dlevel = newNode->level;
}
int r=0;
while (current->nArray[hashpath]->objectarray[r] != NULL && ( r <
num_ob) ){
r++;
}
current->nArray[hashpath]->objectarray[r] = current->objectarray[k];
current->nArray[hashpath]->count++;

/* ignore
countpath = current->nArray[hashpath];
while (countpath->up != NULL)
{
countpath->count++;
countpath = countpath->up; //doesnt add to Sentinel's count
}
Sentinel.count++;
*/
}

hashpath = ML_hash(current->level+1, newObj->key); //hashpath of the
element that i want to insert

if (current->nArray[hashpath]==NULL){
Node< T *newNode = new Node< T >();
current->nArray[hashpath] = newNode;
newNode->up = current;
newNode->level= current->level + 1;
counter++;
if (newNode->level Dlevel)
Dlevel = newNode->level;
}
for(int b=0; b<num_ob; b++)
{
if (current->nArray[hashpath]->objectarray[b] == NULL)
x=1;
//there is space in the node

}

if (x!=1){
current = current->nArray[hashpath];
continue;

}

int r=0;
while (current->nArray[hashpath]->objectarray[r] != NULL && r
< num_ob ){
r++;
}
current->nArray[hashpath]->objectarray[r] = newObj;

countpath = current->nArray[hashpath];
while (countpath->up != NULL)
{
countpath->count++;
countpath = countpath->up;
counter++;
}
Sentinel.count++;
return 1;

}

}
template< typename T >
int MLH< T >::collapse(Node< T *curr, int nn) /*return 0 if no need
to collapse*/
{

for (int f=0; f<num_ob; f++)
{
if (curr->objectarray[f] != NULL) //there is an object in the node
{
return 0;
}
}

if (curr == Sentinel)
return 0;

if (curr->level == Dlevel)
Dlevel--;
delete curr->up->nArray[nn];
curr->up->nArray[nn] = NULL;
curr = curr->up;

int i= 0;

while (curr->up->nArray[i] != curr) //not sure if this works???
{
i++; //location of curr in the nArray of the node above it
}

/*
for(int i = 0; i<num_ob; i++)
{
if (curr->up->nArray[i] == curr) //not sure if this works
break;

}
*/
collapse(curr, i);

}

template< typename T >
void MLH< T >::print()
{
Node< T *tem = &Sentinel;

if (tem->count <= num_ob)
{
for (int i = 0; i < num_ob; i++)
{
if (tem->objectarray[i] != NULL)
{
cout << "Object: " << tem->objectarray[i]->data << " Level: " <<
tem->level << endl;
}
}

}
else{
for (int i = 0; i < num_ob; i++)
{
if(tem->nArray[i] != NULL)
{
print_rec(tem->nArray[i]);
}

}
}
cout << "Number of Objects in list: " << Sentinel.count << endl;
cout << "Deepest node level in repository: "<< Dlevel <<endl;
}
template< typename T >
void MLH< T >::print_rec(Node< T *tem)
{

if (tem->count <= num_ob)
{
for (int i = 0; i < num_ob; i++)
{
if (tem->objectarray[i] != NULL)
{
cout << "Object: " << tem-
>objectarray[i]->data << " Level: " << tem->level << endl;
}
}

}
else{
for (int i = 0; i < num_ob; i++)
{
if(tem->nArray[i] != NULL)
{
print_rec(tem->nArray[i]);
}

}
}
cout << "Number of Objects in list: " << Sentinel.count <<
endl;
cout << "Deepest node level in repository: "<< Dlevel <<endl;
}

Dec 11 '07 #1
4 1510
It looks like u took a shit on your keyboard to make this explosive
piece of glassy shit. You should probably go cut off your hands and
never program again unless u rly like drowning kittens in the sea of
shit u produce.
Dec 11 '07 #2
On 11 Pro, 05:48, rez...@gmail.com wrote:
trying to create a tree of hash nodes, with each node having a max of
5 objects, and a maximum of 5 child nodes...we insert 6 sucesfully and
"explode" to create 3 child nodes, but when it gets to 7 our temp node
has null pointers???

DRIVER PROGRAM::
#include <iostream>
#include "MLH.h"
using std::cout;
using std::endl;

int main()
{
MLH< int tree;

for(int i = 1; i<=300; i++)
{
tree.MLH_insert(i, i );

}

tree.print();

}

MLH.H (data structure)::

#include <iostream>
using std::cout;
using std::endl;
#define Max_levels 8
#include "node.h"
#include "ML_hash.h"

template< typename T >
class MLH
{
friend class Node< T >;
public:
MLH();
int MLH_insert(int, const T &data);
int MLH_delete(int);
int MLH_get(int);
int explode(Node< T *current, Object< T *newObj);
int collapse(Node< T *curr, int);
void print_rec(Node< T *temporary);
void print();
private:
Node< T Sentinel;
Node< T *temp;
int Dlevel;
Node< T nodecount[Max_levels];
int counter;

};

template< typename T >
MLH< T >::MLH()
{
// for (int i = 0; i < Max_levels; i++)
// nodecount[i] = 0;
counter = 0;
Dlevel = 0;

}

template< typename T >
int MLH< T >::MLH_insert(int id, const T &data)
{

temp = &Sentinel;
Node< T *countpath;
int nextnode;

while(1)
{
if (temp->count num_ob) //checks if the node is a stem node
{

nextnode = ML_hash(temp->level + 1, id);
if (temp->nArray[nextnode]==NULL)
{
Node< T *newNode = new Node< T >();
Object< T *newObj = new Object< T >(id, data);

temp->nArray[nextnode] = newNode;
newNode->up = temp;
newNode->level= temp->level +1;
counter++;
if (newNode->level Dlevel)
Dlevel = newNode->level;

temp = newNode;

int r = 0;
while (temp->objectarray[r] != NULL && r<5)
{
r++;
}
temp->objectarray[r] = newObj;

cout<< "You have successfully inserted the
object! Noobsicle!" << endl;
counter++;
countpath = temp;
while (countpath->up != NULL)
{
countpath->count++;
counter++;
countpath = countpath->up;
}
Sentinel.count++;
return 1;
}
else{
temp= temp->nArray[nextnode];

counter++;

// continue;
}
}

if (temp->count < num_ob) /*checks if node has enough space in it*/
{
for(int i = 0; i < num_ob; i++)
{
if (temp->objectarray[i] != NULL)
{
if (temp->objectarray[i]->key == id)
{
cout << "Object is already in list! Noobsicle!" << endl;
counter++;
return 0;

}

}

}
for(int h = 0; h < num_ob; h++)
{
if(temp->objectarray[h] == NULL)
{

Object< T *newObj = new Object< T >(id, data);
temp->objectarray[h] = newObj;

cout<< "You have successfully inserted theobject! Noobsicle!" <<
endl;
counter++;
countpath = temp;
while (countpath->up != NULL)
{
countpath->count++;
counter++;
countpath = countpath->up;
}
Sentinel.count++;
return 1;
}

}
break;
}

if (temp->count == num_ob)
{
//first need to check to see if the element is already there
for(int j = 0; j < num_ob; j++)
{

if (temp->objectarray[j]->key == id)
{
cout << "Object is already in list! Noobsicle!"
<< endl;
counter++;
return 0;
}

}
Object< T *newObj = new Object< T >(id, data);
explode(temp, newObj);//moves the current objects in that node into
another node below it
//then it inserts the object we wish to insert
return 1;
}
}

}

template< typename T >
int MLH< T >::MLH_delete(int id)
{
temp = &Sentinel;
int nextnode;
Node< T *countpath;
int noob;

while(1)
{
if (temp->count num_ob)
{

nextnode = ML_hash(temp->level + 1, id);
temp = temp->nArray[nextnode];
counter++;
continue;
}

if (temp->count < num_ob)
{
for(int i = 0; i < num_ob; i++)
{
if (temp->objectarray[i] != NULL)
{
if (temp->objectarray[i]->key == id)
{
// temp->count--;
countpath = temp;
// noob = i;
while (countpath->up != NULL)
{
countpath->count--;
countpath = countpath->up;
counter++;
}
Sentinel.count--;

delete temp->objectarray[i];

while(temp->objectarray[i+1] != NULL && (i+1) < num_ob ){

temp->objectarray[i] = temp->objectarray[i+1];
i++;
}
temp->objectarray[i] = NULL;
cout << "Object was Deleted! Noobsicle!" << endl;
collapse(temp, nextnode);
counter++;
return 1;

}

}
}
for(int h = 0; h < num_ob; h++)
{
if(temp->objectarray[h] == NULL)
{

counter++;
cout<< "The object you are looking for wasnot found! Noobsicle!"
<< endl;
return 0;
}

}
break;
}

if (temp->count == num_ob)
{
for(int j = 0; j < num_ob; j++)
{

if (temp->objectarray[j]->key == id)
{
// temp->count--;
countpath = temp;
// noob = j;

while (countpath->up != NULL)
{
countpath->count--;
counter++;
countpath = countpath->up;
}
Sentinel.count--;

delete temp->objectarray[j];

while(temp->objectarray[j+1] != NULL && (j+1) < num_ob){
temp->objectarray[j] = temp->objectarray[j+1];

j++;
}
temp->objectarray[j] = NULL;
counter++;
cout << "Object was Deleted! Noobsicle!" <<
endl;
collapse(temp, nextnode);
//run collapse() or should i make check in here?
return 1;
}

}
}
}

}

template< typename T >
int MLH< T >::MLH_get(int id)
{

temp = &Sentinel;
int nextnode;

while(1)
{
if (temp->count num_ob)
{
nextnode = ML_hash(temp->level + 1, id);
counter++;
temp = temp->nArray[nextnode];
continue;
}

if (temp->count < num_ob)
{
for(int i = 0; i < num_ob; i++)
{
if (temp->objectarray[i] != NULL)
{
if (temp->objectarray[i]->key == id)
{
counter++;
cout << "Object was found! Noobsicle!" << endl;
return 1;

}

}
}
for(int h = 0; h < num_ob; h++)
{
if(temp->objectarray[h] == NULL)
{

counter++;
cout<< "The object you are looking for is was not found!
Noobsicle!" << endl;
return 0;
}

}
break;
}

if (temp->count == num_ob)
{
for(int j = 0; j < num_ob; j++)
{

if (temp->objectarray[j]->key == id)
{
cout << "Object was found Noobsicle!" <<
endl;
counter++;
return 1;
}

}
}
}

}

template< typename T >
int MLH< T >::explode(Node< T *current, Object< T *newObj)
{
int hashpath;
int ki;
int x=0;
Node< T *countpath;

while(x != 1){

for (int k = 0; k < num_ob; k++)
{
ki = current->objectarray[k]->key;
hashpath = ML_hash(current->level+1 , ki);
if (current->nArray[hashpath]==NULL){
Node< T *newNode = new Node< T >();
current->nArray[hashpath] = newNode;
newNode->up = current;
newNode->level= current->level +1;
counter++;
if (newNode->level Dlevel)
Dlevel = newNode->level;
}
int r=0;
while (current->nArray[hashpath]->objectarray[r] != NULL&& ( r <
num_ob) ){
r++;
}
current->nArray[hashpath]->objectarray[r] = current->objectarray[k];
current->nArray[hashpath]->count++;

/* ignore
countpath = current->nArray[hashpath];
while (countpath->up != NULL)
{
countpath->count++;
countpath = countpath->up; //doesnt add to Sentinel's count
}
Sentinel.count++;
*/
}

hashpath = ML_hash(current->level+1, newObj->key); //hashpath ofthe
element that i want to insert

if (current->nArray[hashpath]==NULL){
Node< T *newNode = new Node< T >();
current->nArray[hashpath] = newNode;
newNode->up = current;
newNode->level= current->level + 1;
counter++;...

číst dál >>
Your example is quite long for quick answer and I am afraid that
you'll find hardly someone to go through whole code. Try to reduce
your code to minimal size, where the problem still remains. It usualy
leads to identification of problem. When not, try to post some
minimized code again.
Dec 11 '07 #3
re****@gmail.com wrote:
trying to create a tree of hash nodes, with each node having a max of
5 objects, and a maximum of 5 child nodes...we insert 6 sucesfully and
"explode" to create 3 child nodes, but when it gets to 7 our temp node
has null pointers???

Why don't you debug it yourself? You can take a look at valgrind's memcheck.
Dec 11 '07 #4
Xc*******@gmail.com wrote:
It looks like u
Need to be plonked.


Brian
Dec 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: marko | last post by:
I'm having big problems with php and mysql. php segfaults on certain mysql calls, with the following logs in apache error log: child pid 60386 exit signal Segmentation fault (11) and in the...
5
by: Naveen Parihar | last post by:
I've a binary that runs on most of our servers but segfaults on one of the servers. Further, even on this specific machine, the binary runs successfully sometimes but segfaults most of the time....
3
by: Jeffrey Melloy | last post by:
I'm having a problem with the backend occasionally crashing. I have interfaces with the database in two different applications -- a web viewer using JDBC and an insertion routine written using...
1
by: David Douthitt | last post by:
I have a binary statically linked application that was compiled against Red Hat 7.3 (presumably glibc 2.2.5). The application SegFaults when run in a Red Hat Advanced Server environment - with...
4
by: Frank Millman | last post by:
Hi all I am using Python 2.4.1. I have machines running FC4, RH9, and MSW Server 2003 for testing. If I call sax.make_parser() from the interpreter or from a stand-alone program, it works...
3
by: Kentor | last post by:
hello, im trying to make a little loop, but i cant figure it out... i have a string with a bunch of 1s and 0s in it: 110101010101111010101 .... i need to count the number of 1s divide it by 2 and...
2
by: g35rider | last post by:
Hi, I have the following code that is giving this error, I cant simplify the code, I was just testing some theory for something we are doing and was getting an issue here. Please someone point out...
9
by: Josh Zenker | last post by:
I've been working on an implementation of Dijkstra's algorithm on and off for the past few days. I thought I was close to being finished, but clearly I've done something wrong. I'm not a very...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
5
by: chevon1920 | last post by:
I am trying to do my assignment but I cant figure out how to get 8 data points per line to print to a file. Here is the assignment 1. Program asks the user to enter an odd number as a BASE,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.