Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in stack frame diagram? #45

Open
pixelshot91 opened this issue Feb 18, 2024 · 2 comments · May be fixed by #48
Open

Error in stack frame diagram? #45

pixelshot91 opened this issue Feb 18, 2024 · 2 comments · May be fixed by #48

Comments

@pixelshot91
Copy link

In https://highassurance.rs/chp4/assure_stack_1.html#the-stack, the stack diagram show that the stack frame for the square function still exist under all the recursive_count_down stack frames.
https://github.com/tnballo/high-assurance-rust/blob/main/src/chp4/stack_example.svg

But in my understanding, the stack frame of square should pop before the stack frames of recursive_count_down are added.

Indeed if I print the backtrace with:

        println!("Boom!");
        println!("{:#?}", backtrace::Backtrace::capture());
        return x;

I get:

Backtrace [
    { fn: "stack_test::recursive_count_down", file: "./src/main.rs", line: 8 },
    { fn: "stack_test::recursive_count_down", file: "./src/main.rs", line: 13 },
    { fn: "stack_test::recursive_count_down", file: "./src/main.rs", line: 13 },
    { fn: "stack_test::recursive_count_down", file: "./src/main.rs", line: 13 },
    { fn: "stack_test::recursive_count_down", file: "./src/main.rs", line: 13 },
    { fn: "stack_test::main", file: "./src/main.rs", line: 35 },
...

Did I miss something ?

@tnballo
Copy link
Owner

tnballo commented Feb 21, 2024

Nope, you're not missing anything - this is indeed an error in the diagrams!

let _ = recursive_count_down(square(x)); does evaluate square(x) first and pop the frame, before calling recursive_count_down.

I'll fix the diagrams and add a blurb on printing the stack trace as you've described (seems there's no built-in environment variable to get a backtrace on stack overflow automatically, although the aptly-named backtrace-on-stack-overflow crate can help debug this issue in larger programs.

@tnballo
Copy link
Owner

tnballo commented Feb 21, 2024

Also thank you for spotting this error 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants