79520901

Date: 2025-03-19 17:07:32
Score: 4
Natty:
Report link

I am indeed the OP. The answer struck me when I articulated my question here. Thought of posting the answer myself as it might help someone else. I would still be grateful if others add to this answer/point out any mistakes.

The answer is in declaration of hash_t. It is not a 'variable' pointer to type, but rather an array of type. In C we cannot reassign an array name to point to some different location. The code in question does this in the line hash_t hashTable[] = *hashTable_ptr;

Although one thing that I still don't understand is that, if I modify the function definition to:

void FreeHash(hash_t hashTable_ptr)

and then pass the dereferenced pointer to array while calling the function as:

FreeHash(*srptr->symTable);

Then the code works. If someone can answer that I'll be grateful.

This is the corrected code by the way:

void FreeHash(hash_t* hashTable_ptr) {

    varNode *prev, *curr;
    /* freeing each entry */
    for( int i = 0; i < HASHSIZE; i++ ) {
        prev = NULL;
        curr = (*hashTable_ptr)[i];
        while( curr != NULL ) {
            prev = curr;
            curr = curr->next;
            free(prev);
        }
        (*hashTable_ptr)[i] = NULL;
    }

    free(hashTable_ptr);
Reasons:
  • RegEx Blacklisted phrase (2): be grateful
  • RegEx Blacklisted phrase (2): I'll be grateful
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Newton's in-law