Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy this code doesn't allocate memory in C?
    primarykey
    data
    text
    <p>Updated question is here</p> <p><a href="https://stackoverflow.com/questions/828108/please-help-me-to-solve-this-memory-allocation-problem">Memory allocation problem in HashTable</a></p> <p>I'm working on making a HashTable in C. This is what I've done. I think I'm going on a right path but when I'm trying to </p> <p><strong>main.c</strong></p> <pre><code>HashTablePtr hash; hash = createHashTable(10); insert(hash, "hello"); insert(hash, "world"); </code></pre> <p><strong>HashTable.c</strong></p> <pre><code> HashTablePtr createHashTable(unsigned int capacity){ HashTablePtr hash; hash = (HashTablePtr) malloc(sizeof(HashTablePtr)); hash-&gt;size = 0; hash-&gt;capacity = capacity; ListPtr mylist = (ListPtr)calloc(capacity, sizeof(ListPtr)); /* WHY IT DOESN'T ALLOCATE MEMORY FOR mylist HERE?? */ mylist-&gt;head = NULL; mylist-&gt;size = 0; mylist-&gt;tail = NULL; hash-&gt;list = mylist; return hash; </code></pre> <p>ListPtr is a LinkedList ptr</p> <p><strong>List.h</strong></p> <pre><code>typedef struct list List; typedef struct list * ListPtr; struct list { int size; NodePtr head; NodePtr tail; }; ... ... </code></pre> <p><strong>HashTable.h</strong></p> <pre><code> typedef struct hashtable * HashTablePtr; typedef struct hashtable HashTable; struct hashtable { unsigned int capacity; unsigned int size; ListPtr *list; unsigned int (*makeHash)(unsigned int, void *); }; ... ... </code></pre> <p>When I run my debugger, I see no memory being allocated to myList. In above example, my attempt is to make it an array of 10 lists.</p> <p>Please help me to solve this.</p> <p>I'm not that expert in C, if that helps.</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.
 

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