简体   繁体   中英

Basics of Stacks

I had a quick question about stacks that I've been wondering for a little while.

If you give variables a value, and then push them into the stack, will they still contain the same values once they are placed inside the stack, or will the values become "nulled" and you have to alter them within the stack?

The way I understand it, once you pop a variable from a stack, it will return to the value it had before the variable was pushed, correct? But will the variable retain its value when it's pushed as well?

Thanks in advance!

For : Pushing something has no effect on the original, except if you push esp , since the PUSH instruction normally modifies ESP (or sp or rsp , depending on current processor mode).

Pop will overwrite the current content of a register/location with whatever was currently on top of the stack. That'll restore the previous value if and only if your pushes and pops are balanced, so you're

For : Pushing something can destroy the original if you're using move semantics. It's possible to overload things so the same would happen otherwise as well, but it's generally quite inadvisable (to say the least).

Popping only removes an item from the stack. You copy an item from the top of a stack (or a collection you can use in a stack-like fashion, such as vector or deque ), then separately pop to remove that item from the stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM