Reputation: 677
So, as we know Stack works by LIFO principle. As example, we have this code(written by Java, but I think the language is not important):
int a = 1;
int b = 2;
int c = 3;
System.out.println(a);
As i understand firstly a will be pushed to the stack, then b, then c. But at the last line the value of a need to be resolved, stack supports only push and pop operations. Does it mean that for resolving the value of a, the previous values will be pop(ed)?
Does it really fast?
Upvotes: 2
Views: 123
Reputation: 779
This kind of stack is not the same as the LIFO data structure.
The stack in this case refers to a stack frame which contains all of the local variables that the function will be used as well as a return address to the caller. These variables are accessed internally with address offsets from the stack frame pointer. Depending on the calling convention, arguments may be passed into and out of the stack frames, though registers may be used in some cases.
Whenever a function is called, a new stack frame is created and pushed on top of the program stack (or thread stack in multithreaded programs). Since each individual frame has stack semantics, this is why you can normally only access variables that are in the current function scope. (I say normally, because you can sidestep it in C by offsetting pointers to data in the current stack frame, but doing so is compiler-dependent and likely to have disastrous consequences)
When a function returns, the stack frame is popped and execution continues from the new top of the stack. So in that sense, it still retains the LIFO semantics you are familliar with.
Typically, the data on the stack frame is static, though sometimes (for instance some nonstandard functions in C) you can allocate data directly on the stack. This is why C arrays have to be a known size, because they are actually stored on the stack.
Arrays and other data structures are typically stored on the heap, which is another area of memory used for dynamically allocated data (In C, this would be anything created by malloc
; in Java/C++, this would be anything created with new
) The data on the stack is actually just a pointer/reference to the data on the heap, which is why you can return new
ed data to the caller without outright copying it.
Upvotes: 2
Reputation: 726479
Stack data structure used by CPUs and VMs that emulate, including JVM, is not a stack of int
s (or any particular data type, for that matter). It is a stack of so-called stack frames, which hold all local variables and parameters of the current method.
Java decides the layout of method's stack frame at compile time, and uses this information at runtime to make new stack frames when entering a method, and to access local variables from an existing stack frame.
Although the stack indeed supports pushing and popping one item at a time, since the "item" in this case is a stack frame, which contains all three of its local variables of your running method.
Reference: Contents of Stack Frame in Java
Upvotes: 2
Reputation: 211540
The stack isn't something that supports only push and pop operations. You allocate space on the stack for those variables, then populate those values, and can read/interact with them as necessary.
In modern, performant languages those might not even be on the stack at all. They could be registers.
Upvotes: 0