1

I'm trying to make PSET2 crack, however in online IDE I got segmentation fault.

I tried same thing on my local computer and it works fine.

What's wrong? Pls ignore the fact that code is ugly etc. as I had to fight with various problems (I had computer problems and basically forgot lectures' content :()

... (code was here)...

2 Answers 2

2

if(!offset) offset = 0; does nothing. 0 is the value of false, while anything different could be interpreted as true in many contexts (even if it does not equal true, which is 1).

With word[offset=='z'], you probably meant word[offset]=='z'?

I haven't completely understood your algorithm, but as there are 52^4+52^3+52^2+52^1 (+1 for empty string) different alphabetic strings of up to four letters, and you seem to iterate by doing a single tail recursion call, you should run out of call stack pretty soon.

Maybe you could write the algorithm down on paper first (in whatever language you like, but only very simple instructions, as computers are stupid), and step through it with a pen (and make sure to follow the instructions as closely as a computer would - they might be stupid, but they do what you tell them to).

3
  • Thanks for catching this error, however it looks like compiler somehow corrected it.
    – J Doe CS50
    Commented Feb 3, 2017 at 16:06
  • And one more question, what exactly in this kind of algo makes me run out of call stack? Number of times function is called?
    – J Doe CS50
    Commented Feb 3, 2017 at 16:17
  • Each time you call a function, you create a new context on the stack, with local variables and stuff. When you leave the function, the stack pointer is reset and that memory is available again. Since you call the next function without leaving the current, you run out of stack memory after a few calls (might be in the range of a few thousand, depending on number of variables on stack). Since the return value is simply passed from the calling function (the recursive call is the very last thing that the function does), the compiler might change that into a loop.
    – Blauelf
    Commented Feb 3, 2017 at 17:12
2

Ok, I've found answer to c segmentation fault on tail recursion: changing clang option -o from 0 to 3 fixes this problem as this enables better optimization:

clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O3 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow crack.c -lcrypt -lcs50 -lm -o crack

1
  • I created an account just to thank you for this. I've been banging my head against my desk for the past week trying to figure out what was wrong with my code and your answer fixed it for me!
    – CJ5
    Commented Oct 13, 2017 at 13:10

You must log in to answer this question.

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