Re: Stack vs. Heap Question, Please Help

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"arcticool" <arcticool@xxxxxxxxxxx> wrote in message
news:kYudnZTmAMXNCrbZRVn-pA@xxxxxxxxxxxxxxx
Mike,
Thanks for the response, though I think I need an example to grasp
your meaning. I'll see if I can put a framework down and maybe you
could show me where the class would fall off the stack.
Say for example we have a class circle that takes one arguement for
it's radius, and has an area method. It might look something like
this:

public class Circle
{
private int r;
pubic Circle(int radius)
(
r= radius;
}
public int Area()
{
return math.pie * r * r;
}
}

//and we call it from main...
class Test
{
static void main()
{
circle c = new circle(4);
int area = c.Area();
}
}

Could you please say where this would fall off the stack in your
example? Yes I am new to this all, but appreciate any help I can
get.
Thanks again,

Circle is a class, not a struct, so creation of the circle goes on the heap.
(Or so as to not confuse it with the datastructure of the same same, let's
just say the "data pool".) The *reference* to that object is stored on the
stack, inside the stackframe of "main". (Note that if your struct is
*inside* a class instance, the struct is allocated on the heap with the rest
of the class...)
When you exit "main", the stack is unwould and the reference to "c" goes
away, and seeing in this case it's the sole reference to the object in the
memory pool, our circle object will deallocated when the GC finds the time
to do so. (Ignoring any clever optimizations by the compiler.)

For "c" to be allocated on the stack in c#, it would have to be a struct. In
that case, the memory stack allocation would be enough to hold all its
memebers, not just enough for a reference (in this they're the same size)
and if you passed "c" to another function (which you don't in your example)
the enitre object would get *copied* into the stack of the called fn's
stackframe, vs. just the reference to the heap (think "pointer that you
can't change") getting copied in the "class" case. Everything in c# is
passed by value, including object *references*, as long as we ignore ref/out
parameters.

So in contrast to most languages, in c# the stack vs. memory-pool descision
is not made at the point of allocation, but at the point of object (class
vs. struct) definition.

You may also want to look at "using" and "IDispose" - it combines heap
allocation with the scope-based pattern you get with stacks.

m


Jeff
--
----------------------------------------------
Posted with NewsLeecher v3.0 Final
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------


.



Relevant Pages

  • Re: A solution for the allocation failures problem
    ... struct nested *ptr1; ... which would jump to label if the allocation failed. ... implemented for the stack under windows. ... So in the malloc instance I would say you make use of the pre-allocated reserve rather than freeing it so you can do further mallocs whilst recovering. ...
    (comp.lang.c)
  • Re: Optimizing stack access for a stack based VM
    ... Local variables should occur as natural operands of instructions (on ... from the middle of the stack. ... Global stack allocation has the potential for significant savings by ... register allocator with very few global registers does. ...
    (comp.compilers)
  • Re: Fortran memory allocation (stack/heap) issues
    ... > rather than Fortran, ... dynamic allocation, and relatively little stack allocation. ... value return and arrays by reference. ...
    (comp.lang.fortran)
  • Re: Are static array pointers thread safe?
    ... 'static' that it is NOT allocated on the stack! ... how allocation works; I've even had one correspondent assure me that he would not use ... globals, static data members of classes, and local statics. ... observe all sorts of strange things in the absence of synchronization, ...
    (microsoft.public.vc.mfc)
  • Re: Stack
    ... members of the stack at any given time. ... struct stackelement{ ... struct stackelement item; ... The string you were using was an automatic variable. ...
    (comp.lang.c)