0

I am trying to have an array of pointers to generic linked lists within a generic hash table be allocated dynamically with the size being dependent on user input. Please let me show you the code.

This is the driver, the user would input the size that they want the hash table to be which correlates directly to how many linked lists there should be in the hash table.

int main(int argc, char **argv)
{   
int i;
int n;
int count;
unsigned int seed=0;

HashObjectPtr job;
HashTablePtr table;

if (argc < 2) {
    fprintf(stderr, "Usage: %s <table size> [<test size=table size * 10>]);
    exit(1);
}
n = atoi(argv[1]);
count = n;
if (argc >=  3) {
    count = atoi(argv[2]);
    count *= 10;
}
if (argc == 4) {
    seed = atoi(argv[3]);
}
char * firstInput = (char *)malloc(sizeof(char) * 
strlen("I'm a void star made at the beginning") + 1);
firstInput = strcpy (firstInput, "I'm a void star made at the beginning");
table = createHashTable(n, getKey, toString, freeHashObject, compare);

for (i=0; i<n; i++)
{
    job = createHashObject(firstInput);
    HashInsert(table, job);
}

runRandomTests(count, seed, n, table);

if (DEBUG > 0)
    PrintHash(table);
free(firstInput);
FreeHashTable(table);
exit(
}

Here's the struct. I have the array of linked lists defined ListPtr * table => linkedList ** table;

typedef struct HashTable HashTable;
typedef struct HashTable * HashTablePtr;

struct HashTable {
int tableSize;
int (*getKey)(void *);
char *  (*toString)(void *);
void (*freeHashObject)(void *);
Boolean (*compare)(void *, void *);
ListPtr * table;
};

HashTablePtr createHashTable(int size, int (*getKey)(void *), char *  (*toString)(void *), void (*freeHashObject)(void *), Boolean (compare)(void *, void *));
void HashInsert(HashTablePtr table, HashObjectPtr object);
HashObjectPtr HashSearch (HashTablePtr table, HashObjectPtr obj);
void PrintHash(HashTablePtr table);
void FreeHashTable(HashTablePtr table);
HashObjectPtr HashRemove(HashTablePtr table, HashObjectPtr obj);
int HashFunction(HashObjectPtr obj);

This is the function that is initializing the linked lists.

HashTablePtr createHashTable(int size, int (*getKey)(void *), char * (*toString)(void *), void (*freeHashObject)(void *), Boolean (*compare)(void *, void *))
{
HashTablePtr h = (HashTablePtr)malloc(sizeof(HashTable));
h -> tableSize = size;
h -> getKey = getKey;
h -> toString = toString;
h -> freeHashObject = freeHashObject;
h -> compare = compare;

h -> table = (ListPtr *)malloc(sizeof(ListPtr)*size);
int i;
for (i = 0; i < size; i++)
{
    h -> table[i] = createList(getKey, toString, freeHashObject);
}
}

This is the function that creates the linked lists

ListPtr createList(int(*getKey)(void *), 
               char * (*toString)(void *),
               void (*freeHashObject)(void *))
{
ListPtr list;
list = (ListPtr) malloc(sizeof(List));
list->size = 0;
list->head = NULL;
list->tail = NULL;
list->getKey = getKey;
list->toString = toString;
list->freeObject = freeHashObject;
return list;
}

I stepped through this in the debugger in eclipse and it's compiling and running fine but when I am clicking on the "table" variable within h during the CreateHashTable function, everything looks fine, the loop iterates through and createsLists at each index. But when I go to insert, I get a segfault.

It has to be with the way I am initializing the array of ListPointers but I couldn't think of a better way to do.

Help please?

Thank you

Reuben Tanner
  • 5,229
  • 3
  • 31
  • 46
  • Which line causes the segfault? And can you show the definition of `ListPtr`? – luser droog Apr 20 '13 at 23:30
  • The list is as follows: typedef struct list List; typedef struct list * ListPtr; struct list { int size; NodePtr head; NodePtr tail; int (*getKey)(void *); char * (*toString)(void *); void (*freeObject)(void *); }; the line that causes the segfault is in main at HashInsert(table, job); That function looks like this: void HashInsert(HashTablePtr table, HashObjectPtr object) { int key = HashFunction(getKey(object), table -> tableSize); addAtRear(table -> table[key], createNode(object)); } – Reuben Tanner Apr 21 '13 at 05:36
  • I don't know. It's really hard to tell without seeing a complete program. The only thing I can see is that here you call HashFunction with 2 arguments but above it's defined to take 1. – luser droog Apr 21 '13 at 07:26
  • Where is the code of `HashInsert`? If you said that the segfault occurs when you insert elements, so you need to show us that code. – Bechir Apr 21 '13 at 13:09
  • @Bechir I'm almost 100% certain that it is because of the way I am initializing the array of ListPtr's, all the other functions are correct. Could you look at the code under the heading "This is the function that is initializing the linked lists" and see if that looks like the correct way to initialize an array of pointers to structs, assuming the other functions are correct. Thanks. – Reuben Tanner Apr 21 '13 at 14:34
  • @theSilentOne Except that **1** `createHashTable` do not return any value **2** you don't check the `malloc` return value **3** syntax error in the `fprintf` function call; I don't see any problem in your posted code. – Bechir Apr 21 '13 at 15:06
  • @Bechir please look at the declaration of createHashTable again...it returns a HashTablePtr. Yes, you are correct I do not check the malloc return but that shouldn't be the issue and there is no syntax error in fprintf, that's part of my test class and I've ran that separately many times without an issue. – Reuben Tanner Apr 21 '13 at 15:13

0 Answers0