Re: Weird Error with Hashtable Code for Assignment(Maybe Compiler Bug)

2016-11-11 Thread Andrew Pinski
On Fri, Nov 11, 2016 at 10:30 PM, nick  wrote:
> Greetings Gcc Folks,
> Either this is me doing something very simple in a school assignment for 
> creating a basic hashtable
> or a compiler bug. I am currently seeing this error during my tables run on 
> copy constructor call during
> the tester and have set to find out why it doesn't work. After some googling 
> it seems either I am doing
> string pointer to string swaps.

This should have gone to gcc-help@ rather than here.  This mailing
list is more for the automated emails from bugzilla.

Anyways I think the problem is you assume:
new Record*[N];

Will NULL out the array.  It does not.

Thanks,
Andrew


> or it's a compiler bug. This is the error I am getting after running in gdb 
> with backtrace:
> Program received signal SIGSEGV, Segmentation fault.
> 0x77b769bb in std::__cxx11::basic_string std::char_traits, std::allocator 
> >::basic_string(std::__cxx11::basic_string std::allocator > const&) ()
>from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
> (gdb) backtrace
> #0  0x77b769bb in std::__cxx11::basic_string std::char_traits, std::allocator 
> >::basic_string(std::__cxx11::basic_string std::allocator > const&) ()
>from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
> #1  0x004067f6 in HashTable::Record::getKey() const ()
> #2  0x004060ee in HashTable::HashTable(HashTable const&) ()
> #3  0x00403cab in main ()
> This is the complete code for the project:
> #include 
> #include 
> #include
> using namespace std;
> /*The functions I updated for Simple Table are
> 1.search
> 2.grow
> 3.update
> 4.Both Copy Constructor and Copy Assignment
> 5.Destructor
> */
> template 
> class Table {
> public:
> Table() {}
> virtual bool update(const string& key, const TYPE& value) = 0;
> virtual bool remove(const string& key) = 0;
> virtual bool find(const string& key, TYPE& value) = 0;
> virtual ~Table() {}
> };
>
> template 
> class SimpleTable :public Table {
>
> struct Record {
> TYPE data_;
> string key_;
> Record(const string& key, const TYPE& data) {
> key_ = key;
> data_ = data;
> }
>
> };
>
> Record** records_;   //the table
> int max_;   //capacity of the array
> int size_;  //current number of records held
> int search(const string& key);
> void sort();
> void grow();
> public:
> SimpleTable(int maxExpected);
> SimpleTable(const SimpleTable& other);
> SimpleTable(SimpleTable&& other);
> virtual int numRecords();
> virtual bool isEmpty();
> virtual bool update(const string& key, const TYPE& value);
> virtual bool remove(const string& key);
> virtual bool find(const string& key, TYPE& value);
> virtual const SimpleTable& operator=(const SimpleTable& other);
> virtual const SimpleTable& operator=(SimpleTable&& other);
> virtual ~SimpleTable();
> };
>
>
> //returns index of where key is found.
> template 
> int SimpleTable::search(const string& key) {
> int rc = -1;
> for (int i = 0; i if (records_[i]->key_ == key) {
> rc = i;
> break;
> }
> }
> return rc;
> }
> //checks if array is empty
> template 
> bool SimpleTable::isEmpty(){
> return size_==0;
> }
> template 
> //return size of array
> int SimpleTable::numRecords(){
> return size_;
> }
> //sort the according to key in table
> template 
> void SimpleTable::sort() {
> int minIdx = 0;
> for (int i = 0; i minIdx = i;
> for (int j = i + 1; j if (records_[j]->key_ < records_[minIdx]->key_) {
> minIdx = j;
> }
> }
> Record* tmp = records_[i];
> records_[i] = records_[minIdx];
> records_[minIdx] = tmp;
> }
> }
>
> //grow the array by one element
> template 
> void SimpleTable::grow() {
> Record** newArray = new Record*[max_ + 1];
> max_ = max_ + 1;
> for (int i = 0; i newArray[i] = records_[i];
> }
> records_ = std::move(newArray);
> }
>
> /* none of the code in the function definitions below are correct.  You can 
> replace what you need
> */
> template 
> SimpleTable::SimpleTable(int maxExpected) : Table() {
> records_ = new Record*[maxExpected];
> max_ = maxExpected;
> size_ = 0;
> }
>
> template 
> SimpleTable::SimpleTable(const SimpleTable& other) {
> records_ = new Record*[other.max_];
>

Weird Error with Hashtable Code for Assignment(Maybe Compiler Bug)

2016-11-11 Thread nick
Greetings Gcc Folks,
Either this is me doing something very simple in a school assignment for 
creating a basic hashtable
or a compiler bug. I am currently seeing this error during my tables run on 
copy constructor call during
the tester and have set to find out why it doesn't work. After some googling it 
seems either I am doing
string pointer to string swaps.
or it's a compiler bug. This is the error I am getting after running in gdb 
with backtrace:
Program received signal SIGSEGV, Segmentation fault.
0x77b769bb in std::__cxx11::basic_string::basic_string(std::__cxx11::basic_string const&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) backtrace
#0  0x77b769bb in std::__cxx11::basic_string::basic_string(std::__cxx11::basic_string const&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x004067f6 in HashTable::Record::getKey() const ()
#2  0x004060ee in HashTable::HashTable(HashTable const&) ()
#3  0x00403cab in main ()
This is the complete code for the project:
#include 
#include 
#include
using namespace std;
/*The functions I updated for Simple Table are
1.search
2.grow
3.update
4.Both Copy Constructor and Copy Assignment
5.Destructor
*/
template 
class Table {
public:
Table() {}
virtual bool update(const string& key, const TYPE& value) = 0;
virtual bool remove(const string& key) = 0;
virtual bool find(const string& key, TYPE& value) = 0;
virtual ~Table() {}
};

template 
class SimpleTable :public Table {

struct Record {
TYPE data_;
string key_;
Record(const string& key, const TYPE& data) {
key_ = key;
data_ = data;
}

};

Record** records_;   //the table
int max_;   //capacity of the array
int size_;  //current number of records held
int search(const string& key);
void sort();
void grow();
public:
SimpleTable(int maxExpected);
SimpleTable(const SimpleTable& other);
SimpleTable(SimpleTable&& other);
virtual int numRecords();
virtual bool isEmpty();
virtual bool update(const string& key, const TYPE& value);
virtual bool remove(const string& key);
virtual bool find(const string& key, TYPE& value);
virtual const SimpleTable& operator=(const SimpleTable& other);
virtual const SimpleTable& operator=(SimpleTable&& other);
virtual ~SimpleTable();
};


//returns index of where key is found.
template 
int SimpleTable::search(const string& key) {
int rc = -1;
for (int i = 0; ikey_ == key) {
rc = i;
break;
}
}
return rc;
}
//checks if array is empty
template 
bool SimpleTable::isEmpty(){
return size_==0;
}
template 
//return size of array
int SimpleTable::numRecords(){
return size_;
}
//sort the according to key in table
template 
void SimpleTable::sort() {
int minIdx = 0;
for (int i = 0; ikey_ < records_[minIdx]->key_) {
minIdx = j;
}
}
Record* tmp = records_[i];
records_[i] = records_[minIdx];
records_[minIdx] = tmp;
}
}

//grow the array by one element
template 
void SimpleTable::grow() {
Record** newArray = new Record*[max_ + 1];
max_ = max_ + 1;
for (int i = 0; ikey_, other.records_[i]->data_);
}
}
template 
SimpleTable::SimpleTable(SimpleTable&& other) {
size_ = swap(other.size_);
max_ = swap(other.max_);
records_ = swap(other.records_);
}

template 
bool SimpleTable::update(const string& key, const TYPE& value) {
int idx = search(key);
if (idx == -1) {
if (size_ == max_) {
grow();
}