Re: Stack vs. Heap Question, Please Help
- From: "Mike" <vimakefile@xxxxxxxxx>
- Date: Thu, 30 Mar 2006 08:10:04 -0800
"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
*
----------------------------------------------
.
- References:
- Re: Stack vs. Heap Question, Please Help
- From: Mike
- Re: Stack vs. Heap Question, Please Help
- From: arcticool@xxxxxxxxxxx
- Prev by Date: Re: What does strongly typed mean?
- Next by Date: Re: foreach & generics syntax proposal
- Previous by thread: Re: Stack vs. Heap Question, Please Help
- Next by thread: Re: Stack vs. Heap Question, Please Help
- Index(es):