newbee

  • Thread starter Thread starter Bob Grommes
  • Start date Start date
B

Bob Grommes

This is more of a general compiler design question than a C# question.
Suggest you use Google and search on something like:

stack heap tutorial

.... you'll get lots of explanation.

The short answer, though, is that this is universal in all languages ... the
stack is for local variables and provides very fast access via push and pop.
The heap is for larger and/or more complex structures. The heap is slower
to access and more difficult to manage, but is ideal for arbitrary hunks of
data. In C#, reference types (object instance data) is on the heap; value
types are on the stack.

--Bob
 
why do you need a heap and a stack

why not all memory called a heap or call it a stack what is the purpose of
having a heap and a stack
 
This is a post I saved a while ago -

"The heap and the stack are the two places where programs store data (okay,
there are a few more, but they're the two most common ones).

The heap is just a big chunk of memory that is managed by a heap manager (or
garbage collector, in C#'s case). When you write something like:

Employee e = new Employee();

(assuming Employee is a class), you get a chunk of memory allocated out of
the heap to store the information for that employee instance. This instance
will be preserved until it isn't needed anymore (in C#), or until it is
explicitly deleted (in languages like C++).

The stack is used to hold local variables. So, when I write something like:

int i = 15;

as part of a method, that sets aside a chunk of space on the stack to hold
the value "15". When I enter a method, that space is used on the stack, and
when I leave the method, that space is reclaimed for later use.

To be strictly true, in my first example, I not only allocated an instance
of Employee on the heap, I allocated a reference to that on the stack.

Or, to be shorter, all local variables use up space on the stack. If they
are value types, this space is used to store the actual value. If they are
references types, this space is used as a reference (aka pointer) to the
actual value, which is stored on the heap.
--
Eric Gunnerson"


Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
 
why not all memory called a heap or call it a stack what is the purpose of
having a heap and a stack

in addition to the information given by the others:

the heap is a virtually unlimited storage of memory in which all objects are
created. there are a huge amount of occupied blocks of memory separated with
free holes. when a new object is created, a free block of memory has to be
found that is large enough to store the object's data.

the stack is a logically separated fragment of memory (physically it can be
anywhere, even in the middle of the heap, although usually it is somewhere
else). when a method calls another method, it pushes all parameters to that
method on the stack as well as the return address. after the new method is
about to finish, it pops the return address from the stack.

so, the main purpose of the stack is to control the control flow between
method calls (it also speeds the use of method's local variables but it is
not the main purpose).

but why do we need the stack at all?

suppose there's no stack but the heap only. there would be no problem with
passing parameters to consecutive method calls but you would have to SEARCH
the heap for the free block of memory to store the parameters there. this
searching could be SLOW (managing the heap is a bit difficult).

that's why a separated fragment of memory (stack) is used to pass parameters
between methods: you do not have to search for a free block of memory on the
stack, because stack grows and shrinks linearly.

regards,
Wiktor Zychla
 
Back
Top