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
Related
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++) {
I have made a Java like ArrayList class in C for educational purposes however currently it is only good for integers. I want to make it Generic so it can take any type of data. How do I go about it. I read somewhere about creating a typedef void pointer. Any thoughts ?
........................................................................................................
Here is My code
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
typedef struct ArrayList ArrayList;
typedef int bool;
#define false 0
#define true 1
struct ArrayList {
int *con;
int numElements;
int conSize;
};
ArrayList *createArrayList();
void freeArrayList(ArrayList *);
void add(ArrayList *, int);
void printList(ArrayList *);
void resize(ArrayList *);
int remove(ArrayList *, int);
bool isEmpty(ArrayList*);
int getNumElements(ArrayList*);
int getConSize(ArrayList*);
#endif
_____________________________________
#include<stdio.h>
#include<stdlib.h>
#include"Consts.h"
#include "ArrayList.h"
#define CAPACITY 5
ArrayList *createArrayList() {
ArrayList *arrayList = malloc(sizeof(ArrayList));
arrayList->conSize = CAPACITY;
arrayList->numElements = 0;
arrayList->con = malloc(sizeof(int) * CAPACITY);
return arrayList;
}
void freeArrayList(ArrayList * arrayList) {
if (arrayList == NULL) {
return;
}else {
free(arrayList->con);
free(arrayList);
}
}
void add(ArrayList *arrayList, int input) {
//printf("Num elements in add method before adding %d \n", arrayList->numElements);
if (arrayList->numElements >= arrayList->conSize) {
resize(arrayList);
printf("resized\n");
}
int size = arrayList->numElements;
//add element to the last
arrayList->con[size] = input;
arrayList->numElements = arrayList->numElements + 1
}
void resize(ArrayList *arrayList) {
int num = arrayList->numElements;
int oldSize = arrayList->conSize;
int newSize = oldSize + 50;
int *temp = realloc(arrayList->con, sizeof(type) * newSize);
if (temp != NULL) {
arrayList->con = temp;
arrayList->conSize = newSize;
}
}
int remove(ArrayList * arrayList, int val) {
int i = 0;
while (arrayList->con[i] != val) {
i++;
}
//remove this index
if (i == arrayList->conSize) {
return -1;
}
else {
int removedVal = arrayList->con[i]; int j;
for (j = i; j < arrayList->numElements ; j++) {
arrayList->con[j] = arrayList->con[j + 1];
}
arrayList->con[j + 1] = NULL;
arrayList->numElements = arrayList->numElements - 1;
return removedVal;
}
}
If you want the array list be able to store any type of data, you need to
1) make con a void pointer. void *con;
2) store additional metadata in the struct about the memory alignment of the type
3) add one more parameter to the constructor of array list, which is the additional metadata mentioned in 2)
4) when allocating memory, use the stored metadata instead of sizeof(whatever), like temp=malloc(stored_metadata_about_type*50);
Also notice that it is not usually a good idea to hardcode 50, and better declare it as a constant like buffer_size.
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");
}
I have a pointer to a pointer to a structure and I am trying to access a pointer-to-pointer pointer within that structure. I keep getting an error that reads: "request for member 'buckets' in something not a structure or union." I commented next to the error. So my question is, how do I properly access buckets and allocate memory for it.
typedef struct bucket {
char *key;
void *value;
struct bucket *next;
} Bucket;
typedef struct {
int key_count;
int table_size;
void (*free_value)(void *);
Bucket **buckets;
}
int create_table(Table ** table, int table_size, void (*free_value)(void *)){
int iterate = 0;
*table = malloc(sizeof(Table));
if(table && table_size != 0) {
(*table)->key_count = 0;
(*table)->table_size = table_size;
(*table)->free_value = free_value;
(*table)->buckets = malloc(table_size * sizeof(Bucket)); /* Error is here */
while(iterate < table_size)
*table->buckets[iterate++] = NULL;
return SUCC;
}
return FAIL;
}
By the look of your allocation:
(*table)->buckets = malloc(table_size * sizeof(Bucket));
It looks as though you are attempting to create space for table_size buckets and not table_size pointers to bucket. It the allocation should read:
(*table)->buckets = malloc(table_size * sizeof(Bucket*));
The error, I believe, is further down in the while loop. Operator -> has precedence over [] which has precedence over *, therefore you are actually saying *(table->buckets[iterate++]), and table is a pointer-pointer and does therefore not have a member called bucket.
(Note: You're missing a Table; in your second typedef.
int create_table(Table ** table, int table_size, void (*free_value)(void *)){
int iterate = 0;
*table = malloc(sizeof(Table));
if(table && table_size != 0) {
This isn't right. You should test table before you allocate memory, and then test *table.
(*table)->key_count = 0;
(*table)->table_size = table_size;
(*table)->free_value = free_value;
(*table)->buckets = malloc(table_size * sizeof(Bucket)); /* Error is here */
Here, you are allocating space for table_size number of Bucket, but assigning the memory to a pointer to pointer to Bucket. It looks like you want to allocate table_size number of pointers to Bucket.
while(iterate < table_size)
*table->buckets[iterate++] = NULL;
This is the same as *(table->buckets[iterate++]) which is obviously wrong. table is not a pointer to struct, *table is. This is where your real error is.
I would probably write something like this:
typedef struct {
int key_count;
int table_size;
void (*free_value)(void *);
Bucket **buckets;
} Table;
int create_table(Table **table, int table_size, void (*free_value)(void *))
{
if (!table || table_size == 0) {
return FAIL;
}
*table = malloc(sizeof(Table));
if (*table) {
(*table)->key_count = 0;
(*table)->table_size = table_size;
(*table)->free_value = free_value;
(*table)->buckets = malloc(table_size * sizeof(Bucket *));
if ((*table)->buckets) {
int iterate = 0;
while(iterate < table_size)
(*table)->buckets[iterate++] = NULL;
return SUCC;
}
}
if (*table) {
free ((*table)->buckets);
}
free (*table);
return FAIL;
}
I'm trying to figure out how to initialize an array of pointers to linked lists that has a dynamic size based on user input.
I've got a struct as follows:
struct HashTable {
int tableSize;
int (*getKey)(void *);
char * (*toString)(void *);
void (*freeHashObject)(void *);
Boolean (*compare)(void *, void *);
ListPtr table;
};
The table variable is supposed to contain a dynamic number of linked lists depending on user input. You can assume that n is a valid integer and that createHashObject works as it is supposed to.
HashTablePtr table;
HashObjectPtr job;
table = createHashTable(n, getKey, toString, freeHashObject, compare);
for (i=0; i<n; i++)
{
job = createHashObject(firstInput);
HashInsert(table, job);
}
I believe the problem lies in createHashTable which is as follows.
HashTablePtr createHashTable(int size, int (*getKey)(void *),
char * (*toString)(void *), void (*freeHashObject)(void *),
Boolean (*compare)(void *, void *))
{
HashTablePtr h = (HashTablePtr)malloc(sizeof(HashTable));
//dont worry that i dont do things with the function pointers
h -> table = (ListPtr)malloc(sizeof(List)*size);
int i;
for (i = 0; i < size; i++)
{
h -> table[i] = createList(getKey, toString, freeHashObject);
}
}
The above code doesn't seem to correctly initialize all the linked lists. Here is the code for the createList function and the List struct.
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;
}
struct list {
int size;
NodePtr head;
NodePtr tail;
int (*getKey)(void *);
char * (*toString)(void *);
void (*freeObject)(void *);
};
I am willing to wager that the issue lies in how I am defining the dynamically sized array of pointers to the linked lists and then initializing it. Can anyone offer me some help with that?
Thank you.
In your HashTable structure change the table definition to declare it as pointer, as below
struct HashTable {
...
ListPtr *table;
};
Then you can allocate as many you want which you have done correctly.