I'm having a really hard time understanding why we use malloc to get memory instead of just using the regular int n = 0; way. Typically when we want to make an int, we type int n; not int* n = malloc(sizeof(int));. However, in the walkthrough, they write node* n = malloc(sizeof(node)); instead of node n;. Is there are reason we can't do the latter?

The answer I've gotten to this question is you need malloc in case you don't know ahead of time how much memory you need. I don't understand when this would be the case. Is Speller one of those times?

My understanding of the "load" function is as follows:

For each current_word in the dictionary:

  1. Hash it to find the right spot in the table.

  2. Follow the linked list to the end.

  3. Make a new node to contain current_word and connect the node to the end of the list.

To me it seems step 3 is doable without malloc. Couldn't we just do something like:

node n;

strcpy(n.word, current_word);

And then have the last node in the linked list point at this new node?

1 Answer 1


Stack allocated memory is automatically released when the function it's allocated in returns. We want the data from the dictionary to stay in memory after we run load.

  • I see. So if I wrote something like node n; n.word = "blah"; in a function, that node would disappear once the program leaves the function?
    – H_R
    Feb 9, 2022 at 15:16
  • @H_R correct. That memory is released. Feb 9, 2022 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .