By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

Heap/Stack Allocations - Are my assumptions correct?

P: n/a
I have a question related to allocating on the stack.

In this program below, are my following assumptions true?

- variable a is allocated on the heap since it's static
- variable b is allocated on the stack since it's a value type variable
- variable d is allocated on the stack since it's a value type variable

Where does variable "c" get allocated? It's a value type, but not foundwithin a method.

using System;

namespace MyTest
{
class Class1
{
static int a = 0; // Allocated on the heap.

static void Main(string[] args)
{
int b = 0; // Allocated on the stack.
b = a;
}
}

class Class2
{
int c = 5; // Allocated where?

public int Test(int X)
{
int d = 1; // Allocated on the stack.

return d * X * c;
}
}
}

-Nick McCamy

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
a is allocated when your class is first loaded into memory, and it will
always remain allocated (unless the assembly gets unloaded)
I don't know exactly where it gets allocated but it is probably more in some
area called "static data" than on the heap itself (it won't be GCed). It is
not on the stack, unless you qualify it with the [ThreadStatic] attribute,
in which case it will be at the very bottom of the stack.

b is allocated on the stack, inside the current stack frame.

c is allocated inline inside the instances of Class2, which are themselves
allocated on the heap. So, c is indirectly allocated on the heap.

d is allocated on the stack, nside the current stack frame.

So, the statement saying that instances of value types are allocated on the
stack is wrong. In the case of c, the value is allocated indirectly on the
heap. And, in the case of a, I don't know exactly where it gets allocated
(could be on the heap, could be in some "static data" area), but it does not
matter because it will always be there.

Bruno.

"Nick McCamy" <nm*****@carolina.rr.com> a écrit dans le message de
news:20042216827.969865@lexundesigns...
I have a question related to allocating on the stack.

In this program below, are my following assumptions true?

- variable a is allocated on the heap since it's static
- variable b is allocated on the stack since it's a value type variable
- variable d is allocated on the stack since it's a value type variable

Where does variable "c" get allocated? It's a value type, but not found
within a method.

using System;

namespace MyTest
{
class Class1
{
static int a = 0; // Allocated on the heap.

static void Main(string[] args)
{
int b = 0; // Allocated on the stack.
b = a;
}
}

class Class2
{
int c = 5; // Allocated where?

public int Test(int X)
{
int d = 1; // Allocated on the stack.

return d * X * c;
}
}
}

-Nick McCamy
Nov 15 '05 #2

P: n/a
Nick McCamy <nm*****@carolina.rr.com> wrote:
I have a question related to allocating on the stack.

In this program below, are my following assumptions true?

- variable a is allocated on the heap since it's static
Yes.
- variable b is allocated on the stack since it's a value type variable
- variable d is allocated on the stack since it's a value type variable


No - b and d are both allocated on the stack because they're local
variables.

Variables themselves are:

- On the stack if...
They are instance variables of a struct, and the struct itself is
on the stack.
They are local variables.

- On the heap if...
They are instance variables of a reference type.
They are instance variables of a struct, and the struct itself is
on the heap.
They are static variables.

Note that the value of a reference type variable is a reference - so
while the object itself will always live on the heap, the reference can
live either in the stack or in the heap, depending on the above rules.

See http://www.pobox.com/~skeet/csharp/memory.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.