1

so I do realize this is my 3rd post regarding pset5, but I really want to understand it. These are the three questions that I could not answer:

  1. My code for load() function uses fscanf to go through the dictionary. I created the variable char word[45] to save the word into when it gets fscanfed. How do I make it so that the memory for word is allocated dynamically thus I can save more memory?
  2. Why do you think we pass before and after by reference (instead of by value) to calculate, even though we’re not changing their contents? I think it is so that copies of the variables do not get created on stack, thus saving memory. I think that is wrong for sure, could someone answer it for me please?
  3. Why does load() and check() use const char*? Is it so that I cannot modify the data I am being passed in the function, because that would modify the data in the dictionary as well?

Thank you guys.

1

How do I make it so that the memory for word is allocated dynamically thus I can save more memory?

functions that enable you to dynamically allocate memory on the heap are malloc, calloc, and realloc. see the man pages for these functions for more information.

I think it is so that copies of the variables do not get created on stack, thus saving memory. I think that is wrong for sure, could someone answer it for me please?

why do you think this is wrong for sure?

Why does load() and check() use const char*? Is it so that I cannot modify the data I am being passed in the function, because that would modify the data in the dictionary as well?

not really. when a variable is const, that means that you can't change its value once it's assigned. the case is slightly different for pointers since they store data (memory addresses) and point to data. if the keyword const is before the name of the pointer type (as it's the case here), that means that the data that this pointer points to is constant (can't be modified).

if you recall, char *s are pointers, meaning a function is able to modify the actual data that a char * points to. having const before a char * ensures that the function can't modify the data through this char *.

what does this have to do with the dictionary file? absolutely nothing. the concern here is about the data that the parameters point to not being modified by the functions.

because load shouldn't modify the path passed to it and check shouldn't modify the word passed to it, these parameters have been qualified by const to enforce the rules (i.e., attempts to modify these parameters inside these functions are forbidden by the compiler).

2
  • Thank you Kareem, now it is clear to me. Regarding the second question, I said for sure because I actually did not believe that it could be the right answer, is it though? May 16 '16 at 21:50
  • yes, indeed. passing a variable by value causes a creation of a copy of the contents of that variable on the stack, which would be unnecessary in this case. if this answers your question, mind accepting the answer to mark the question as solved?
    – kzidane
    May 16 '16 at 21:52

You must log in to answer this question.

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