Error Code 139
Sep 19, 2014 at 3:39pm UTC
Hello, I am implementing a Hash Table based on a given function. I have coded most of it, but whenever I try to add something to the hash table, I get an error code of 139. I've narrowed it down to where it crashes (HashTab.cpp in the add() function), and I would appreciate if someone looked over my code and offered suggestions or a solution to make it not crash anymore. Here is the 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 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
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include "HashTab.h"
using namespace std;
void runPrompt();
void addelm(int num);
void remelm(int num);
void search(int num);
void printelm();
void error(string s);
HashTab* hashTab;
//this is where i will store the string tokens
string strarray[10];
int main()
{
runPrompt();
}
void runPrompt()
{
string input = "" ;
int index;
while (input != "quit" )
{
index = 0;
cout << "set> " ;
//get the input and split into iss stream
getline(cin,input);
istringstream iss(input);
while (iss)
{
//gather strings delimited by ' ' and store in array
//index starts at 0, so increment after
iss >> strarray[index];
index++;
}
if (input.find("add" ) != string::npos)
{
//add num aka strarray[1]
int numadd = atoi(strarray[1].c_str());
addelm(numadd);
}
if (input.find("delete" ) != string::npos)
{
//remove
//strarray[0] = remove
//[1] = num
int numrem = atoi(strarray[1].c_str());
remelm(numrem);
}
if (input.find("print" ) != string::npos)
{
printelm();
}
if (input.find("search" ) != string::npos)
{
int searchnum = atoi(strarray[1].c_str());
search(searchnum);
}
if (input.find("quit" ) != string::npos)
{
break ;
}
if (input.find("quit" ) == string::npos && input.find("print" ) == string::npos
&& input.find("delete" ) == string::npos && input.find("add" ) == string::npos
&& input.find("search" ) == string::npos)
{
error(input);
}
}
}
void printelm()
{
hashTab->print();
}
void addelm(int num)
{
hashTab->add(num);
}
void remelm(int num2)
{
hashTab->rem(num2);
}
void search(int num)
{
hashTab->search(num);
}
void error(string input)
{
cout << "Error! Command not found: " << input <<endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "Element.h"
class HashTab
{
public :
HashTab();
void print();
void add(int );
bool rem(int );
bool search(int );
private :
Element* elarray[7];
};
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
#include <iostream>
#include "Element.h"
#include "HashTab.h"
using namespace std;
HashTab::HashTab()
{
}
void HashTab::add(int num)
{
//Error checking to see if number is within range
if (num < 0 || num > 10)
{
cout << "Error! number is out of range (0-9)" << endl;
return ;
}
//hash x^2 mod B where B=7
else
{
int key = (num*num) % 7;
cerr << key << endl;
for (int i = 0; i < 9; i++)
{
if (elarray[key]->getindex(i) == -1)
{
cerr << "this is where it faults\n" ;
elarray[key]->setindex(i);
break ;
}
else
{
cout << "Bucket full.\n" ;
}
}
}
}
bool HashTab::rem(int num)
{
for (int i = 0; i < 6; i++)
{
if (elarray[i]->search(num))
{
elarray[i]->del(num);
return true ;
}
}
//no match
return false ;
}
bool HashTab::search(int num)
{
for (int i = 0; i < 6; i++)
{
if (elarray[i]->search(num))
{
cout << "true" << endl;
}
}
}
void HashTab::print()
{
for (int i = 0; i < 9; i++)
{
cout << "(" ;
elarray[i]->printinfo();
cout << ")\n" ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef ELEMENT_H
#define ELEMENT_H
#include <iostream>
#include <string>
using namespace std;
class Element
{
public :
Element();
void setindex(int );
void printinfo();
void del(int );
bool search(int );
int getindex(int );
private :
int idarray[10];
};
#endif // ELEMENT_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
#include "Element.h"
using namespace std;
Element::Element()
{
for (int i = 0; i < 9; i++)
{
cerr << i << endl;
idarray[i] = -1;
}
}
int Element::getindex(int num)
{
return idarray[num];
}
void Element::printinfo()
{
for ( int i =0; i < 9; i++)
{
if (idarray[i] == -1)
{
cout << " ," ;
}
else
{
cout << idarray[i] << "," ;
}
}
}
bool Element::search(int num)
{
bool flag = false ;
for (int i = 0; i < 9; i++)
{
if (idarray[i] == num)
{
flag = true ;
}
}
return flag;
}
void Element::del(int num)
{
for (int i = 0; i < 9; i++)
{
if (idarray[i] == num)
{
idarray[i] = -1;
}
}
}
void Element::setindex(int num)
{
bool fullflag = true ;
for (int i = 0; i < 9; i++)
{
//not used
if (idarray[i] == -1)
{
idarray[i] = num;
fullflag = false ;
break ;
}
if (fullflag)
{
cout << "Error! The bucket for number " << num << " is full!" << endl;
break ;
}
}
}
Please let me know what I am doing wrong.
Sep 19, 2014 at 5:36pm UTC
"Error 16" is meaningless. Error numbers are different for every compiler.
You also don;t make clear if this is a compiler error, or a run time error.
Please post the full error text of the message including the source filename and line number where the error occurred.
Sep 19, 2014 at 6:00pm UTC
Sorry! It's a run time error. The program itself executes fine. Here is the output when I try to use the add() function.
1 2 3
set>add 1
Program exited with code 139
Sep 19, 2014 at 6:07pm UTC
HashTab* hashTab;
This defines a pointer to a
HashTab . Later you dereference the pointer without ever making it point to anything valid.
1 2 3 4 5 6
class HashTab
{
// ...
private :
Element* elarray[7];
};
This defines an array of pointers to
Element in each HashTab instance. Later you use this array without making those pointers point to anything valid.
Sep 19, 2014 at 9:51pm UTC
So just do something like this?
HashTab* hashTab = new HashTab():
and
Element* elarray = new Element[7];
Pointers are still very new to me, and I was under the impression that
HasTab* hashTab
would declare and create a valid pointer to use.
Sep 19, 2014 at 10:07pm UTC
So just do something like this?
HashTab* hashTab = new HashTab();
Something like it.
and
Element* elarray = new Element[7];
elarray as defined in the code above is an array of 7 pointers-to-
Element . You could change the definition to something like described here, or you could keep it as you previously had it and make those pointers point to objects, or you could just use a normal array of 7
Element objects.
Pointers are still very new to me, and I was under the impression that
HasTab* hashTab
would declare and create a valid pointer to use.
It defines a pointer. Given that it is defined at global scope, it is zero-initialized. It is not valid to dereference as it does not point to an existing object.
The thing to remember about pointers is that you shouldn't use them unless you can't get around it.
Last edited on Sep 19, 2014 at 10:07pm UTC
Sep 21, 2014 at 5:25am UTC
Cool. I kept the HashTab as a pointer and made my Element array out of objects instead. Works like a charm, thank you cire and AbstractionAnon!
Topic archived. No new replies allowed.