Note that there are some explanatory texts on larger screens.

plurals
  1. POInitialize double pointer of structs
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <pre><code>int main(int argc, char **argv) { int i; int n; int count; unsigned int seed=0; HashObjectPtr job; HashTablePtr table; if (argc &lt; 2) { fprintf(stderr, "Usage: %s &lt;table size&gt; [&lt;test size=table size * 10&gt;]); exit(1); } n = atoi(argv[1]); count = n; if (argc &gt;= 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&lt;n; i++) { job = createHashObject(firstInput); HashInsert(table, job); } runRandomTests(count, seed, n, table); if (DEBUG &gt; 0) PrintHash(table); free(firstInput); FreeHashTable(table); exit( } </code></pre> <p>Here's the struct. I have the array of linked lists defined ListPtr * table => linkedList ** table;</p> <pre><code>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); </code></pre> <p>This is the function that is initializing the linked lists.</p> <pre><code>HashTablePtr createHashTable(int size, int (*getKey)(void *), char * (*toString)(void *), void (*freeHashObject)(void *), Boolean (*compare)(void *, void *)) { HashTablePtr h = (HashTablePtr)malloc(sizeof(HashTable)); h -&gt; tableSize = size; h -&gt; getKey = getKey; h -&gt; toString = toString; h -&gt; freeHashObject = freeHashObject; h -&gt; compare = compare; h -&gt; table = (ListPtr *)malloc(sizeof(ListPtr)*size); int i; for (i = 0; i &lt; size; i++) { h -&gt; table[i] = createList(getKey, toString, freeHashObject); } } </code></pre> <p>This is the function that creates the linked lists</p> <pre><code>ListPtr createList(int(*getKey)(void *), char * (*toString)(void *), void (*freeHashObject)(void *)) { ListPtr list; list = (ListPtr) malloc(sizeof(List)); list-&gt;size = 0; list-&gt;head = NULL; list-&gt;tail = NULL; list-&gt;getKey = getKey; list-&gt;toString = toString; list-&gt;freeObject = freeHashObject; return list; } </code></pre> <p>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.</p> <p>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. </p> <p>Help please?</p> <p>Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload