OT: Theory of stack and heap allocation of variables

C

Chris Dunaway

I have seen on these newsgroups many posts about value types and
reference types. And, generally speaking, value types are allocated on
the stack, and reference types on the heap.

My question is about the stack and heap itself. Is the stack really a
stack? is ia LIFO data structure? Does each program have its own
stack?

What happens when a value type is dim'ed? What is actually stored on
the stack and how is it organized? When a reference type is dim'd what
happens? Is the reference itself stored in the stack at compile time
and then the object created at runtime?

Can you point to any articles that explain the theory of how types are
allocated on the stack and heap as well as some details about how the
stack and heap works? I am just trying to find an article that
explains it. Not necessarily how it works in .Net, but in general,
although a .Net explanation would be helpful.

Thanks,

Chris
 
C

Chris Dunaway

Thanks for the article, it is very interesting.

I am looking for a little more however. For example suppose I have the
following two declarations:

Dim i As Integer
Dim j As Integer

Since integer is a value type, these are stored on the heap.

What if I access them in the following order:

Console.WriteLine(i.ToString)
Console.WriteLine(j.ToString)

Since they're on the stack and i was place on the stack first, and then
j, does it have to pop j off the stack to get to i and then push j back
on?

I'm trying to understand a little about how the memory is allocated and
how it is accessed and that fits with the LIFO stack.

Thanks again for the article,
 
R

Ross Presser

Thanks for the article, it is very interesting.

I am looking for a little more however. For example suppose I have the
following two declarations:

Dim i As Integer
Dim j As Integer

Since integer is a value type, these are stored on the heap.

ITYM "on the stack", both from what you said earlier in the thread and what
you say later in this message. Value types are allocated on the stack.
What if I access them in the following order:

Console.WriteLine(i.ToString)
Console.WriteLine(j.ToString)

Since they're on the stack and i was place on the stack first, and then
j, does it have to pop j off the stack to get to i and then push j back
on?

No. You misunderstand slightly.

For a value type to be stored on the stack, this means that its physical
location in memory will be on the stack, right inbetween callback return
addresses and other stack frames. BUT, the code that accesses the value
types does not have to push and pop to get to them. It can just use an
index from the stack pointer - and the compiler knows exactly what index to
use, because it remembers what order they came in.

The benefit of putting values on the stack is that when the program
returns, they are automatically freed - no garbage collection is necessary.
However, it is only practical for value types, because they are small and
fixed in size. Reference types, being complex objects, vary greatly in how
much storage they need, and of course they can persist between calls, so
the stack is an inappropriate place to keep them.
I'm trying to understand a little about how the memory is allocated and
how it is accessed and that fits with the LIFO stack.

Thanks again for the article,

Hope this helped. I should mention that I may be partly or totally wrong,
since I'm not speaking from actual knowledge of how the CLR works, but from
my memory of the alloca() function from my old days programming in C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top