Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How did I access the middle element of the stack if you can't access the middle element

I’m reading this document, and it says that The stack stores values in the order it gets them and removes the values in the opposite order. This is referred to as last in, first out. Think of a stack of plates: when you add more plates, you put them on top of the pile, and when you need a plate, you take one off the top. Adding or removing plates from the middle or bottom wouldn’t work as well!

So how does this work

fn main() {
    let _x = 1;
    let y = 2;
    let _z = 3;
    println!("{}", y);
}

Thanks for helping, sorry if I’m missing something obvious.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Every function needs a specific amount of RAM to hold all variables in this function. These are called frames. Frames are stacked, not the variables in them.
Every time you call a function, a new frame is allocated. Every time you leave a function, the frame for this function is dropped.

The Function-Stack has nothing to do with accessing variables in the same frame.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading