0

I'm trying to create a class for a HashTable implementation

Since I'm doing a chained HashTable, my hashTable starts out as a an array of pointers of type "object".

What I am having trouble with is my constructor since in my main file, I'm going to take in a value for the size of the array. Like this:

int N;
scanf("%d", &N);
//Create HashTable Object
HashTable *hash = new HashTable(N);

This is my .h file:

class HashTable {

    int arraySize;

    typedef struct object {
        string data;
        object *nextptr;
    } object;

    object** table;
public:
    //Constructor
    HashTable(int size);

    /// ...and other methods...

For my constructor implementation I keep getting error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

So I know I'm doing something wrong.

This is my Constructor implementation.

//Constructor
HashTable :: HashTable(int size){
    this->arraySize = size;
    this->table = new object[size]; //<-- this is giving me issues!!

    for (int i = 0; i < arraySize; i++) {
        table[i] = new object;
        table[i]->data = "";
        table[i]->nextptr = NULL;
    }

}

If someone could help me out here that'd be greatly appreciated... new to this sort of stuff.

EDIT: The assignment requires us to use arrays and not vectors.

I think I need to use an array of pointers since every index in the array is going to hold a linked list of objects that have "collided" with the same index.

GusGus
  • 230
  • 6
  • 16
  • 1
    Don't use new. Use `HashTable hash(N);` and vectors. It's acceptable, indeed preferred, to implement new data structures in terms of simpler, efficient and debugged ones if possible. – Neil Kirk Mar 22 '15 at 03:30
  • 1
    You are using an indirection level too many. You could simply make your table an `object*` (i.e. an array of objects) and dispense with each object allocation. Better yet, you could use a `std::vector` of objects, to avoid all the unpleasant pitfalls of raw arrays. – kuroi neko Mar 22 '15 at 03:34
  • Yeah I would use vectors if I could but the assignments require the use of arrays. I'll edit it and clarify that point. So `HashTable hash(N);` instead of `HashTable *hash = new HashTable(N);` in my main? @NeilKirk – GusGus Mar 22 '15 at 03:36
  • 1
    I never understand these assignments. There's nothing to stop you wrapping arrays in your own homebrew vector-like data structure, which would be better design if vectors really weren't available in the real world. But another assignment teaching bad practices.. – Neil Kirk Mar 22 '15 at 03:38
  • @NeilKirk Indeed, I've never understood the point of forcing use of a data structure in a problem where using a different data structure would be preferable. If you want to teach about arrays, pick a problem where an array is a logical choice to solve the problem (like, say, implementing a `vector`, because then you'd both learn how to use arrays and why using `vector` or other standard containers where someone else already solved all the edge cases is the right choice 90% of the time). Even worse, this assignment seems to be cramming both arrays and linked-lists into one. – aruisdante Mar 22 '15 at 03:44
  • I agree!! But nevertheless I still need to follow directions.... I was thinking about @kuroineko 's suggestion of using an array of objects but the reason its an array of pointers of objects is because each index in the array is a linked list. Am I wrong that it needs to be of pointers rather than objects? – GusGus Mar 22 '15 at 03:53
  • `this->table = new object[size];` If this gives you issues, check size is a sensible value. – Neil Kirk Mar 22 '15 at 03:55
  • Yes! `this->table = new object[size];` compiles! but... my methods don't work properly anymore since its an array of objects now and i can't treat them like an array of linked lists, which is what I need. @NeilKirk – GusGus Mar 22 '15 at 03:58
  • Also, note that I declared my table variable as `object** table` so `this->table = new object[size];` wouldn't work @NeilKirk – GusGus Mar 22 '15 at 04:01
  • Writing a basic `MyVector` is easier than messing with manual array management. – Yakk - Adam Nevraumont Mar 22 '15 at 04:07

1 Answers1

0

Found out the answer to my own question.... For anyone else wondering the answer to my own question:

this->table = new object *[size];

this line would dynamically create an array of pointers to "object" objects.

In the header file the decleration is:

object** table;
GusGus
  • 230
  • 6
  • 16