Heap/Stack Allocations - Are my assumptions correct?

N

Nick McCamy

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
 
B

Bruno Jouhier [MVP]

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" <[email protected]> a écrit dans le message de
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
 
J

Jon Skeet [C# MVP]

Nick McCamy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top