What does LIFO really mean?


As mentioned in this tutorial: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

In computer programming, a stack is a container that holds other variables (much like an array). However, whereas an array lets you access and modify elements in any order you wish, a stack is more limited. The operations that can be performed on a stack are identical to the ones above:

1) Look at the top item on the stack (usually done via a function called top()) 2) Take the top item off of the stack (done via a function called pop()) 3) Put a new item on top of the stack (done via a function called push())

But if I defined two variables in C++ I do not have to use them in the same order of definition:


int main() {
 int a;
 int b;

 b = 5;
 a = 6;

Is there a problem on this code? I can use them in any order I like!! I do not have to use the a first then the b.

Am I misunderstanding something? What is it?

You are confusing two different kinds of stacks.

One stack, is where some of the memory for your application is allocated. This would be part of a discussion about the stack and heap and where your memory is allocated.

The other kind of stack is a data structure that conforms to a LIFO style of access. This could be implemented using a std::vector or other forms of data structures.