WSBT
WSBT

Reputation: 36323

How can variables be retrieved at any time if they are allocated onto the stack?

To my understanding, each thread usually gets only one stack (while all threads in a process usually share a heap). I always thought that stack is used for storing the value of Program Counter(PC) when function call happens. But then I read somewhere that certain variable types such as integer or boolean are allocated onto the stack as well. Since values on stacks are managed in a strict FILO manner, how can these variables be retrieved at any time?

For example, after declaring int a, b, c;, I can do whatever I want to these variables in any order at any time within their scope. How is this done? Why isn't value c on the top of the stack and therefore hiding values a, b?

Upvotes: 2

Views: 112

Answers (3)

StuartLC
StuartLC

Reputation: 107247

The call stack is also used for local variables, and also for passing parameters into a function.

You are right in that 'Value types' are passed on the stack, whereas reference types are allocated on the heap, but when a reference type is used as a parameter, a pointer to this heap location will be still passed on the stack.

Although stacks are usually 'perceived' as LIFO buffers, there are also frame or base pointers associated with the call stack, which can be used to directly access memory above or below the current stack pointer. This is how functions can still have random access to parameters without changing the stack pointer.

This diagram from Wikipedia may help visualize this, although note that many would argue that the stack should grow 'downwards'.

This blog post here explains Intel call stacks

Upvotes: 1

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

There are lots of answers to that question, depending on what environment you are working in. Certainly you shouldn't equate the processor stack as in (used with Machine code POP and PUSH) instructions with the stack (or more correctly stackframe) used by something like a python interpreter or .net runtime

But the short answer is the top of the stack is simply a memory location, so you simply use an offset

if you did Push a,b,c

So the address of b would be stack address - 4.

Upvotes: 1

Pyrce
Pyrce

Reputation: 8571

First off I'd suggest that you should research the stack in general a bit more (see Wikipedia and Stack Explanation and others via googling/CS books).

However for your specific question, all variables that aren't heap generated are on the stack. This usually includes anything that you define within a particular function which isn't directly heap allocated (via new operator in C++ and Java). In some languages everything is heap allocated and only pointers to those heap structures are stored on the stack (like in Python). You'll see a variety of these minor changes between languages.

Thus your statement about integer and boolean always being on the stack is incorrect. They are on the stack if you define them within a function and on the heap if you build them with new. Note that with Java if you use an int primitive it is usually stack based and Integer objects are heap based -- but this is a more advanced nuance of Java beyond basic stack knowledge.

You can access int a, b, c; all within the same scope because it makes room for all 3 variables within that function stack scope. When the function returns these variables are cleaned up as the stack is moved back up. Until that point all 3 are present because the whole scope block at once is part of the FILO structure of the stack and will persist until you return.

Upvotes: 1

Related Questions