Re: Stack vs. Heap Question, Please Help

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1e9625e7d10c6dd98cff1@xxxxxxxxxxxxxxxxxxxxxxx
Nick Hounsome <nh002@xxxxxxxxxxxxxxxxxx> wrote:

<snip>

Re. The stack - the only practical difference between stack and heap at
the
app level is that stack is faster. This is solely because logically it
cannot become fragmented or need GC. I believe that there are or were
micro-controllers that didn't have one.

I don't know - I think that the idea of variables being on the stack or
on the heap (along with the concept of stack frames) is very important
when it comes to understanding threading, and why if two threads are
running the same method at the same time (or even a single thread
running a method recursively) they get separate local variables, but if
they access the same object on the heap, that data is shared.

struct parameters and structs within methods are thread local as
(recursively) are any structs they contain directly. Everything else is
shared. You don't need to use the words stack or heap to explain this.

P.S. It is totally possible to implement a stack in a stackless
architecture - just pass a pointer to the array of arguments and allocate
frames off the heap - of course you then have a "stack" but it is a
different sort of stack and not as efficient for aloocation although the
deallocation efficiency still applies.


.



Relevant Pages

  • Re: Memory (Structure vs Class)
    ... Such a block of memory which is ... actually so can structs ... heap while structures would be the stack unless declared global. ...
    (microsoft.public.vb.general.discussion)
  • Re: Difference between nullable class and nullable<> structure
    ... The heap, in general, is open memory, while the stack is program memory. ... Now, anyone who has programmed for a good length of time will note that this statement is not absolutely 100% correct and possibly even misleading, but it is very useful when explaining why a person who writes only structures and no classes is more apt to end up with an out of stack memory condition in his program because he heard that structures are faster. ... For a program using exclusive structs as the user-defined types to have the persistent instances of those structs allocated on the stack, you'd have to make a new method call for each object you want to allocate, and that method would have to not return until you no longer need that object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Stack vs. Heap Question, Please Help
    ... on the heap (along with the concept of stack frames) is very important ... they access the same object on the heap, ... are any structs they contain directly. ... You don't need to use the words stack or heap to explain this. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Groovy (war Re: [PROST]Re: CDC Plugin fuer Eclipse)
    ... Warum müsste man continuations erlauben Stackvariablen in den Heap zu verschieben? ... Ein activation record speichert die Parameter und lokalen Variablen der Funktion und eine Information, ... Da diese activation records klassischerweise auf einem Stack verwaltet werden, kann man hier Stackspace sparen - Endrekursion ist damit genauso effizient wie eine klassische Iteration mit einem Sprung. ...
    (de.comp.lang.java)