How to use pointers correctly in C - c

I am trying to add (key, value) pairs to a hashmap but cannot access the values after insertion.
This hash table is supposed to deal with collisions as I am iterating along each hash index whenever a collision occurs. I then insert it when I have reached the end of the (key, value) pair list at that index.
Essentially it is a basic linked list hashmap.
The problem is, I keep getting a segmentation fault when I try to access the value again (and my showTable() function also fails). In this test, I am simply trying to access the first (key, value) pair at each hash index after something is added at that hash index. I am probably doing something very silly but I see it.
I have not yet commented but I hope the code is self explanatory. The important bit is InsertKeyValuePair() but I have added everything as a code review would also be beneficial.
#include <stdlib.h>
#include <stdio.h>
typedef struct TVal KeyValue;
typedef struct TVal {
char *value;
char *key;
KeyValue *next;
} KeyValue;
typedef KeyValue **HashTable;
int MAX_SIZE = 200;
int HashKey(char *Key, int Max);
void InsertKeyValuePair(char *key, char *value, int Index, HashTable table);
int insert(char *Key, char *value, HashTable table, int size);
void showTable(HashTable table, int size);
int HashKey(char *Key, int Max) {
char c = *Key;
int Hash = 0;
int n = 1;
while (c != 0) {
Hash += n * ((int)c);
c = *(Key + n);
n++;
}
return Hash % MAX_SIZE;
}
void InsertKeyValuePair(char *key, char *value, int Index, HashTable table) {
KeyValue *cursor = *(table + Index);
while (cursor != NULL) {
cursor = cursor->next;
}
cursor = malloc(sizeof(KeyValue));
cursor->value = value;
cursor->key = key;
printf("insert <K,V>(%s,%s) HashIndex = %i\n", cursor->key, cursor->value, Index);
//Trying to access value previously inserted
KeyValue *cursor2 = *(table + Index);
printf("<K,V>(%s,%s)\n", cursor2->key, cursor2->value);
}
int insert(char *Key, char *value, HashTable table, int size) {
int Index = HashKey(Key, MAX_SIZE);
InsertKeyValuePair(Key, value, Index, table);
return size + 1;
}
void showTable(HashTable table, int size) {
int i;
for (i = 0; i < size; i++) {
KeyValue *cursor = *(table + i);
if (cursor == NULL)
continue;
while (cursor != NULL) {
printf("==============");
printf("<K,V>(%s,%s)\n", cursor->key, cursor->value);
cursor = cursor->next;
}
printf("==============");
}
}
int main() {
HashTable HTbl = malloc(sizeof(HashTable) * MAX_SIZE);
int size = 0;
size = insert("yeuydfdan", "wesfg", HTbl, size);
size = insert("ywere", "rdgg", HTbl, size);
size = insert("ye4", "3244", HTbl, size);
//showTable(HTbl, MAX_SIZE);
}

There are multiple problems in your code:
The hash table is not initialized to NULL, causing segmentation faults when trying to dereference the pointers it contains. Allocating with calloc() would fix this problem.
It is confusing and error prone to hide pointers behind typedefs.
The allocation in main should read HashTable HTbl = calloc(sizeof(*HTbl), MAX_SIZE);
the insertion code in InsertKeyValuePair does not link the new pair at the end, nor at the beginning of the hashtable bucket list.
it is advisable to use unsigned arithmetics to compute the hash key to avoid overflow issues.
the pointer notation *(table + Index) is confusing. You should use the array notation table[Index] instead.
there seems to be some confusion between the length of the hashtable (MAX_SIZE) and the number of entries in the hashtable (size). Renaming the variables appropriately may improve readability. It is also probably better to pass the count by address and return a success indicator.
Here is a corrected version:
#include <stdlib.h>
#include <stdio.h>
typedef struct TVal KeyValue;
typedef struct TVal {
const char *value;
const char *key;
KeyValue *next;
} KeyValue;
typedef KeyValue **HashTable;
static unsigned int HASH_SIZE = 200;
static unsigned int HashKey(const char *key);
static KeyValue *InsertKeyValuePair(const char *key, const char *value, int index, HashTable table);
static int insert(const char *Key, const char *value, HashTable table, int *countp);
static void showTable(HashTable table);
static unsigned int HashKey(const char *key) {
unsigned int hash = 0;
size_t n;
for (n = 0; key[n] != 0; n++) {
hash += n * (unsigned char)key[n];
}
return hash % HASH_SIZE;
}
static KeyValue *InsertKeyValuePair(const char *key, const char *value, int index, HashTable table) {
KeyValue *cursor;
cursor = malloc(sizeof(KeyValue));
if (cursor != NULL) {
KeyValue **cursorp = &table[index];
while (*cursorp != NULL) {
cursorp = &(*cursorp)->next;
}
cursor->value = value;
cursor->key = key;
cursor->next = NULL;
*cursorp = cursor;
}
return cursor;
}
static int insert(const char *key, const char *value, HashTable table, int *countp) {
int index = HashKey(key);
if (InsertKeyValuePair(key, value, index, table)) {
*countp += 1;
return 1;
}
return 0;
}
static void showTable(HashTable table) {
unsigned int i;
for (i = 0; i < HASH_SIZE; i++) {
KeyValue *cursor = table[i];
if (cursor == NULL)
continue;
while (cursor != NULL) {
printf("==============");
printf("<K,V>(%s,%s)\n", cursor->key, cursor->value);
cursor = cursor->next;
}
printf("==============\n");
}
}
int main() {
HashTable HTbl = calloc(sizeof(*HTbl), HASH_SIZE);
int count = 0;
insert("yeuydfdan", "wesfg", HTbl, &count);
insert("ywere", "rdgg", HTbl, &count);
insert("ye4", "3244", HTbl, &count);
showTable(HTbl);
return 0;
}

This statement
HashTable HTbl = malloc(sizeof(HashTable)*MAX_SIZE);
is incorrect and moreover the allocated memory is not initialized. There should be
HashTable HTbl = calloc( MAX_SIZE, sizeof( KeyValue * ) );
or like
HashTable HTbl = calloc( MAX_SIZE, sizeof( *HTbl ) );
The index within the table should be calculated as some unsigned integer. Otherwise in general you can get a negative index.
In function HashKey parameter Max is not used.
In the function InsertKeyValuePair there is changed the local variable cursor instead of the data member cursor->next or *(table+Index).
The loop in the function showTable shall use MAX_SIZE not size in the loop condition. That is you have to pass as an argument the value of MAX_SIZE not the value of size.
Here is a demonstrative program that shows how the program can be updated.
#include <stdio.h>
#include <stdlib.h>
typedef struct TVal KeyValue;
typedef struct TVal
{
const char *value;
const char *key;
KeyValue *next;
} KeyValue;
typedef KeyValue **HashTable;
const size_t MAX_SIZE = 200;
static size_t HashKey( const char *key, size_t max_slots )
{
size_t Hash = 0;
for ( size_t i = 0; key[i]; i++ ) Hash += ( i + 1 ) * ( unsigned char )key[i];
return Hash % max_slots;
}
static int InsertKeyValuePair( HashTable table, const char *key, const char *value, size_t index )
{
KeyValue **cursor = &table[index];
while ( *cursor != NULL ) cursor = &( *cursor )->next;
*cursor = malloc( sizeof( KeyValue ) );
int success = *cursor != NULL;
if ( success )
{
( *cursor )->value = value;
( *cursor )->key = key;
( *cursor )->next = NULL;
}
return success;
}
int insert( HashTable table, const char *key, const char *value, size_t *size )
{
size_t index = HashKey( key, MAX_SIZE );
int success = InsertKeyValuePair( table, key, value, index );
if ( success ) ++*size;
return success;
}
void showTable( HashTable table, size_t size )
{
for ( size_t i = 0; i < size; i++ )
{
KeyValue *cursor = table[i];
if ( cursor != NULL )
{
do
{
puts( "==============" );
printf( "<K,V>(%s, %s)\n", cursor->key, cursor->value );
cursor = cursor->next;
} while ( cursor != NULL );
puts( "==============\n" );
}
}
}
int main( void )
{
HashTable HTbl = calloc( MAX_SIZE, sizeof( *HTbl ) );
size_t size = 0;
insert( HTbl, "yeuydfdan", "wesfg", &size );
insert( HTbl, "ywere", "rdgg", &size );
insert( HTbl, "ye4", "3244", &size );
showTable( HTbl, MAX_SIZE );
}
The program output is
==============
<K,V>(ywere, rdgg)
==============
==============
<K,V>(ye4, 3244)
==============
==============
<K,V>(yeuydfdan, wesfg)
==============
Of course you should add some other functions as for example a function that deletes the table with its nodes.
And it will be better if each node will allocate memory for a key and a value and copy there the passed arguments. Otherwise the table may deal in general only with string literals because they have static storage duration.
If you will rewrite the implementation of table such a way that it will copy keys and values in nodes of the table then the structure should be defined like
typedef struct TVal KeyValue;
typedef struct TVal
{
char *value;
const char *key;
KeyValue *next;
} KeyValue;
That is in any case the key should not be changed and should be declared with the qualifier const.

Related

Inserting an item to dynamic dictionary is not working

As a learning exercise, I'm trying to implement my own dictionary in C. My dictionary type definition is:
#define MAXCAP 24
static size_t primes[MAXCAP + 2] = {
1,
101, 251, 509, 1021, 2039, 4093, 8191,
16381, 32749, 65521, 131071, 262139, 524287, 1048573,
2097143, 4194301, 8388593, 16777213, 33554393, 67108859, 134217689,
268435399, 536870909, 10737441789, 2147483647};
typedef struct item_tag item;
struct item_tag {
void *key;
void *val;
item *next;
};
typedef struct dict_tag {
size_t cap; // capacity of the dict, which is used as an index for primes[]
size_t size; // number of slots taken up out of the capacity of the dict
item **items;
int (*eq) (const void *, const void *);
int (*hash) (const void *, size_t n);
} dict;
My function for inserting a new entry to the dict is:
int dict_put(void *key, void *val, dict *d) {
int i;
item *kv;
if (!dict_get(key, d)) {
kv = malloc(sizeof(item));
kv->key = key;
kv->val = val;
kv->next = d->items[(i = d->hash(key, primes[d->cap]))];
d->items[i] = kv;
if (!kv->next)
d->size++;
if (d->size >= primes[d->cap] / 2)
expand(d);
return 1;
}
return 0;
}
Insertion works fine if I do not try to resize the dict using expand function which is defined as:
static void expand(dict *d) {
int i;
item *kv;
dict *tmp;
if (d->cap < MAXCAP) {
tmp = malloc(sizeof(dict));
init(d->cap + 1, d->hash, d->eq, tmp);
for (i = 0; i < d->cap; i++) {
for (kv = d->items[i]; kv; kv = kv->next)
dict_put(kv->key, kv->val, tmp);
}
destroy_items(0, d);
d->cap = tmp->cap;
d->size = tmp->size;
d->items = tmp->items; // looks like there are no items in dict after this step
free(tmp);
} else
fprintf(stderr, "dict: max size reached.\n");
}
In the above function, I'm trying to create a new temporary larger dict and then copy the pointer to the new list of items to the old dict. The init function is:
static void init(size_t n, const int (*hash) (const void *, size_t n), const int (*eq) (const void *, const void *),
dict *d) {
d->cap = n;
d->size = 0;
d->eq = eq;
d->hash = hash;
d->items = calloc(primes[d->cap], sizeof(item *));
}
Your problem is on this line in the expand function
for (i = 0; i < d->cap; i++) {
should be changed to
for (i = 0; i < primes[d->cap]; i++) {

Hashtable with linked list not work in c?

I've a problem with memory allocation for an hash table with linked list (for avoid collisions) in C.
I think that the problem is on allocation of an item.
I've made two scruct, one for the single item and one for the table.
The first have two pointer to next and prev item.
Please help me.
I stay on this code until 3 days.
The code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAPACITY 50000
unsigned long hash(char *str) {
unsigned long int stringsum = 0;
for(; *str != '\0'; str++) {
stringsum += *str;
}
return stringsum % CAPACITY;
}
typedef struct item {
char *value;
char *key;
struct item *next;
struct item *prev;
} ht_item;
typedef struct hashtable {
ht_item **items;
int dim;
int count;
} HashTable;
HashTable* create_table(int size); HashTable* create_item(HashTable *table, char *value, char *key);
void print_table(HashTable* table, int dim);
int main(void) {
HashTable *table = create_table(CAPACITY);
table = create_item(table, "Giuseppe", "Nome");
print_table(table, CAPACITY);
return 0;
}
HashTable* create_item(HashTable *table, char *value, char *key) {
unsigned long index = hash(key);
printf("%u", index);
ht_item *_iterator; ht_item *prev;
for(_iterator = table->items[index], prev = NULL; _iterator != NULL; prev = _iterator, _iterator = _iterator->next);
_iterator = (ht_item*)malloc(sizeof(ht_item));
_iterator->key = (char*)malloc(200);
_iterator->value = (char*)malloc(200);
strcpy(_iterator->key, key);
strcpy(_iterator->value, value);
_iterator->next = NULL;
_iterator->prev = prev;
return table;
}
HashTable* create_table(int size)
{
HashTable *table = (HashTable*)malloc(sizeof(HashTable));
table->dim = size;
table->items = (ht_item**)calloc(size, sizeof(ht_item*));
for(int i = 0; i < size; i++){
table->items[i] = NULL;
}
return table;
}
void print_table(HashTable* table, int dim) {
for(int i = 0; i < CAPACITY; i++)
{
if(table->items[i] != NULL)
{ ht_item *_iterator = (ht_item*)malloc(sizeof(ht_item));
for(_iterator = table->items[i]; _iterator != NULL;
_iterator = _iterator->next)
{
printf("Key: %s\tValue: %s\n", _iterator->key, _iterator->value);
} free(_iterator);
}
}
}
Made some changes in your code. Please read through the blocks containing // CHANGE HERE comment.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAPACITY 50000
// CHANGE HERE - additional parameter, value to be used for modulo
unsigned long hash(char *str, unsigned int mod_value) {
unsigned long int stringsum = 0;
for(; *str != '\0'; str++) {
stringsum += *str;
}
// CHANGE HERE - use mod_value instead of CAPACITY
return stringsum % mod_value;
}
typedef struct item {
char *value;
char *key;
struct item *next;
struct item *prev;
} ht_item;
typedef struct hashtable {
ht_item **items;
int dim;
int count;
} HashTable;
HashTable* create_table(int size); HashTable* create_item(HashTable *table, char *value, char *key);
void print_table(HashTable* table, int dim);
int main(void) {
HashTable *table = create_table(CAPACITY);
table = create_item(table, "Giuseppe", "Nome");
print_table(table);
return 0;
}
HashTable* create_item(HashTable *table, char *value, char *key) {
// CHANGE HERE - function arguments validation
if (table == NULL)
{
return table;
}
if (value == NULL || key == NULL)
{
printf("Key or value is null\n");
return table;
}
// CHANGE HERE - pass table->dim to hash
unsigned long index = hash(key, table->dim);
printf("Index: %lu\n", index);
// CHANGE HERE - simplified the code a bit
ht_item* new_node = malloc(sizeof(ht_item));
new_node->key = malloc(200 * sizeof(char));
strncpy(new_node->key, key, 200);
new_node->value = malloc(200 * sizeof(char));
strncpy(new_node->value, value, 200);
// CHANGE HERE - if first node in index
if (table->items[index] == NULL)
{
table->items[index] = new_node;
return table;
}
ht_item *cur, *prev = NULL;
for(cur = table->items[index]; cur != NULL; prev = cur, cur = cur->next);
prev->next = new_node; // CHANGE HERE - it seems this line was missing
new_node->prev = prev;
new_node->next = NULL;
return table;
}
HashTable* create_table(int size)
{
HashTable *table = (HashTable*)malloc(sizeof(HashTable));
table->dim = size;
table->items = (ht_item**)calloc(size, sizeof(ht_item*));
for(int i = 0; i < size; i++){
table->items[i] = NULL;
}
return table;
}
void print_table(HashTable* table) {
// CHANGE HERE - function arguments validation
if (table == NULL)
{
printf("Table is null\n");
return;
}
// CHANGE HERE - change CAPACITY to dim
for(int i = 0; i < table->dim; i++)
{
//printf("i = %d [%d]\n", i, table->items[i] == NULL);
if(table->items[i] != NULL)
{
// CHANGE HERE - removed unnecessary malloc
ht_item *_iterator = NULL;
for(_iterator = table->items[i]; _iterator != NULL; _iterator = _iterator->next)
{
printf("Key: %s\tValue: %s\n", _iterator->key, _iterator->value);
}
}
}
}
The create_item function can and should be simplified.
I have put some comments inline.
HashTable* create_item(HashTable *table, char *value, char *key) {
// use modulo operator here, not in the hash function
unsigned long index = hash(key) % table->dim;
// nicer way of allocating
ht_item *insert = malloc(sizeof *insert);
// use strdup to avoid wasted memory and buffer overflows
insert->key = strdup(key);
insert->value = strdup(value);
// head insert rather than tail
insert->next = table->items[index];
table->items[index] = insert;
return table;
}
I dropped the use of the prev member. If you need that somewhere it's an exercise for you to add it. I don't think it's necessary for a simple hash table.

C Hash Table with String Keys.All keys are the last string in the file

I'm trying to implement a hash table with keys of strings. I don't care about the values, so they are ints of 1. I need the speed of a hash look-up so no binary searches in arrays. My key strings are not being stored correctly, they print as expected when they are inserted, but when I display the hash table's contents, the keys are all the last entry in the file.
Please help
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 103141 * 2
#define MAXCHAR 1000
struct DataItem {
int data;
char* key;
};
struct DataItem* hashArray[SIZE];
unsigned long
hashCode(char *key) {
unsigned long hash = 5381;
int c;
while (c = *key++){
hash = ((hash <<5)+ hash)+c;
}
return hash % SIZE;
}
struct DataItem *search(char *key) {
unsigned long hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key){ return hashArray[hashIndex]; }
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void insert(char* key,int data) {
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
unsigned long hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != NULL) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
// this statement below prints the keys correctly
printf("%d %s %d\n",hashIndex,item->key,item->data);
hashArray[hashIndex] = item;
}
void display() {
int i = 0;
for(i = 0; i<SIZE; i++) {
if(hashArray[i] != NULL)
// here the keys are all "S-4TNKZ"
printf(" (%s,%d)\n",hashArray[i]->key,hashArray[i]->data);
else
printf(" .. \n");
}
}
int main() {
struct DataItem* item;
char *probefile = "cychp_pruned_r2_0.1.ids.txt";
FILE *probef; char str[MAXCHAR]; char *p; char *probe_name;
probef = fopen(probefile,"r");
while (fgets(str, MAXCHAR, probef) != NULL){
p = strtok(str,"\n");
insert(p,1);
}
display();
// this exists in the file
char *probe = "S-4TNIU";
// if I insert it here, it works
//insert(probe,1);
// not found
item = search(probe);
if(item != NULL) {
printf("Element found:%s %d\n", probe,item->data);
} else {
printf("Element not found [%s]\n",probe);
}
}

Hash table init_hash in c

I need to initialized the hash table with the size i get, i have a problem here t->arr_table[i]->key = NULL;
#include <stdio.h>
typedef struct element{
char * key;
char * value;
}element;
typedef struct HashTable{
int size; // size of the arr
element **arr_table; //arr of elements
}HashTable;
void init_hash(int size, HashTable * t)
{
if (size < 1)
return;
t->size = size;
t->arr_table = (element **)malloc(sizeof(element*)*size);
if (t->arr_table == NULL) // out memory
return;
int i;
for (i = 0; i < size; i++)
{ // initial list
t->arr_table[i]->key = NULL;
t->arr_table[i]->value = NULL;
}
}
void main()
{
HashTable *ht = (HashTable*)malloc(1*sizeof(HashTable));
int size_ht = 9;
init_hash(size_ht, ht);
printf("...\n");
return;
}
What you've made is an array of pointers to elements. However, the init_hash function seems to expect an array of elements. To create an array of elements the code should be as shown below. I've added some comments to highlight some of the changes.
typedef struct element{
char *key;
char *value;
}element;
typedef struct HashTable{
int size;
element *arr_table; // <-- only one '*', not two, to declare a pointer to an array of elements
}HashTable;
void init_hash(int size, HashTable *t)
{
if (size < 1)
return;
t->size = size;
t->arr_table = malloc(sizeof(element) * size); // <-- allocate memory for the elements, note 'sizeof(element)' not 'sizeof(element *)'
if (t->arr_table == NULL)
return;
int i;
for (i = 0; i < size; i++)
{
t->arr_table[i].key = NULL; // <-- table[i] is a structure, use dot notation
t->arr_table[i].value = NULL;
}
}
int main( void ) // <-- declare main with the correct signature
{
HashTable *ht = malloc(sizeof(HashTable)); // <-- don't cast the return value from malloc
int size_ht = 9;
init_hash(size_ht, ht);
printf("...\n");
}

Initialize double pointer of structs

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

Resources