Select random word: Storage/memory issue


I have the following issue:
First, it works but playing the game again, only “null” is stored in selected2. I presume this is some memory issue but I’m not sure why it occurs. When running make and playing the game again, it might not even work the first time.

This seems indeed like a memory issue.
Write unit tests for generateDict with debug print statements to look for ASAN errors and to inspect the strings.
Make sure that the char pointer of the second word is still alive (and not freed or dead) at the necessary locations.

Thanks for the recommendation. I implemented a unit test now, which prints the contents of selected1 and selected2. For the file 5_simpl.txt, the output is always “hotel” and “cigar”, i.e. there doesn’t seem to be any randomness involved. That seems weird to me because when playing the game from the command line, the words were different over several runs.

To figure out what the reason might be, I wanted to print the random numbers generated by drand48() to check whether they stay the same too. However, these numbers are not returned by the generateDict function so I don’t know how I should print them (in contrast to selected1 and selected2 which exist outside of the generateDict function). Do you have any recommendation on how to best tackle this issue?

A similar issue occurs when using the file “5.txt”: The selected1 word is always the first word of the file, namely “cigar”. On the other hand, when trying to print information about selected2, I get an “exotic error”.
When googling this issue, most advice seemed to be about fixing Python code and I’m not sure how I could understand why my code produces this error. Overall, it’s somewhat insightful that I can see more detailed error messages but I’m not sure how to continue from here…

The error likely happens since the memory you read is uninitialized.

I would recommend debugging your test to precisely see what happens.

Ok, I don’t quite understand :grimacing:. So there is some error in my unit_test involving uninitialized memory? I actually structured my test analogously to the “load_simple” test and then I just added some fprintf statements as suggested by Marcel here: Load_simple public test - #3 by jato00001. That test worked fine and I didn’t make any special initializations there.

Well, yes, It seems like there is. Maybe because you are not writing to the pointer after all?

You could print a few random numbers in the test and notice that they are always the same.
That is due to the facts that you have to seed the randomness as done in main: