Veera P
Veera P

Reputation: 11

how stack memory can be used?

In stack the memory can be stored in a LIFO manner then I will show u an example to explain my question clearly

public static void main()
{
  int i=0;
  char ch='a';
  string s="doubt";
}

In this above example first stack stores 'i' value and on the top of that stores 'ch' value and on the top of that 's' value stores. Now the question is if I want to use 'i' value how can it be popped out. If it pops all the variables to get int 'i' value, where are the 's' and 'ch' values, can be stored

Upvotes: 0

Views: 84

Answers (2)

Java implementations generally compile source code to some JVM bytecode. C# implementations compile for the CLI, that is to CLR bytecode called CIL.

The JVM bytecode is for a stack based machine. The CIL bytecode is also stack-based. So both JVM & CIL bytescodes target some stack machine (but a different one for Java -i.e.JVM- and for C# -i.e. CIL-).

The compiler would compute the set of local variables and generate appropriate instructions to deal with the current frame in the call stack.

Conceptually, the slot (in that current call frame) for a local variable is popped when the control flow gets out of the block declaring it (but what happens in reality is an implementation detail). Read about scope. So you don't need to explicitly pop any value (the compiler takes care of nested scopes and local variables).

Quite often, a call frame is pushed (on the call stack) during call in the function prologue and is popped (from the call stack) in the (corresponding) function epilogue, but the details depend upon the calling conventions.

The JVM requires a garbage collector and so does the CIL. It would scan the pointers (to objects) on the stack (in your example s)

BTW, Java has some reflection facilities (not for newbies) which may give access to the call stack. See this. C# (and the CIL) also provides reflection. Read also about continuations.

The call stack is indeed a stack following a last-in first-out discipline, but the pull and pop operations are part of the calling protocol and are compiler generated so remain implicit. During a call, a new call frame is pushed on the call stack. When returning, it is popped off the call stack. Read also about tail-calls (then at call time the new call frame is replacing the current one); sadly the JVM don't support tail-calls in general.

I recommend reading SICP which has some chapters explaining these notions. It is an excellent freely available introduction to programming (which does not use Java or C# as its programming language).

Upvotes: 1

user207421
user207421

Reputation: 311050

In this above example first stack stores 'i' value and on the top of that stores 'ch' value and on the top of that 's' value stores.

Correct. Note that you didn't say 'push', which would not have been correct.

Now the question is if i want to use 'i' value how can it be popped out.

It isn't 'popped out'. It is accessed via an index from the current stack frame base.

If if pops all the variables to get int 'i' value

It doesn't.

where are the 's' and 'ch' values can be stored

On the stack, or better still in the current stack frame.

You should think of the stack as a stack of stack frames, one per method invocation, rather than as a value stack.

Upvotes: 1

Related Questions