View Full Version : stack, heap and C#
mickey
18th August 2007, 16:18
Hello, I read that C# allow 2 types: value and reference. Class object are always reference types; "struct" and base type (int, float, bool) are value types; but I read that if I embed any value types in a reference type (eg an int32 inside a class declaration), all goes on the heap (even the int32); outside of an object that int32 should go on the stack (the variable with its value).
class Program {
struct Address {
public Address (string v, int n) {
this.street = v;
this.number = n;
}
internal string street;
internal int number;
};
Address addr = new Address ("donner",99);
public Int32 m = 9;
static void Main(string[] args) {
System.Int32 i = 99;
}
}
Above I see 'm' and 'addr' be value types: it should be on the stack. But reading better I can see that they're inside the class 'Program'; I think that Someone create an instance of class 'Program' (I don't know who and where); but the question is: in the manner of C#, every type is inside a Class declaration; so everything goes on the heap; but I know that it isn't possible; then, what happen exactly??? I hope you understand...
thanks..
marcel
18th August 2007, 18:16
I think you misunderstood the meaning of value and reference types in C#.
Value types contain directly their data, while reference types are very similar to pointers in C++. A reference is exactly what the name suggests: a reference to some object on the heap.
So, in your example, even if you instantiate that struct in the class scope, it will not be a reference. The data contained by it will be placed in a contiguous block at a certain offset in the memory allocated on the heap for the CLASS.
So the class data itself is allocated on the heap, but the struct will not have its own heap memry allocated. Instead it uses the class space -- some kind of static allocation.
If you were to allocate a class instead of the struct, then class Program would actually contain a reference to your other class, which would be allocated on the heap.
But using a struct makes class Program containing the actual data in its own memory blocks.
regards
mickey
18th August 2007, 18:47
sorry, but I don't understand. I'm very confused. I read value types go on the stack and ref type on the heap (I hope this right):
//MyClass class is defined elsewhere
class Program {
struct Address {
public Address (string v, int n) {
this.street = v;
this.number = n;
}
internal string street;
internal int number;
};
MyClass cl = new myClass();
Address addr = new Address ("abbay",99);
public Int32 m = 9;
static void Main(string[] args) {
System.Int32 i = 99;
}
}
AFAIK the main() method must be inside a class (e.g. Program). So I repeat the previous question: Does all that is inside Program class go on the heap?
If yes, I'm wondering a declaration of any type that goes on the stack; could you insert in my program above a declaration that will be allocate on the stack?
Probabily i'm still doing wrong....
marcel
18th August 2007, 18:59
Ok. Once again you're missing the essential.
//MyClass class is defined elsewhere
class Program {
struct Address {
public Address (string v, int n) {
this.street = v;
this.number = n;
}
internal string street;
internal int number;
};
MyClass cl = new myClass(); //Allocated on the heap. Will have its own heap space
Address addr = new Address ("abbay",99); //Will be allocated directly in the Progream class heap space since it is a value type. addr does not point to a separate heap location but to a location relative to the classes memory space base address.
public Int32 m = 9; //Value type, therefore allocated in class space
static void Main(string[] args) {
System.Int32 i = 99; // allocated on main() stack
Address a = new Address("x", 10); //allocated on the current function's stack
}
}
Does all that is inside Program class go on the heap?
Yes but depends... Value types go on the class's heap, while ref types get their own heap.
Regards
mickey
18th August 2007, 23:28
//error to remove, please
mickey
18th August 2007, 23:38
Sorry......
//myclass.cs
MyClass {
public int a;
public void swapl(ref x, ref y) {
int temp;
temp = x;
x = y;
y = temp;
}
}
Class Program {
.........................................
static void Main(string[] args) {
System.Int32 i = 99, u = 10; // 1
MyClass simple = new MyClass(); //2
simple.swap (i,u); //XX
Address a = new Address("x", 10); // 3
}
}
//1 - value type; allocate on the stack because local variabile of a function (call
function)
//2 - ref type; local class; it'll die with end of main(); on the stack of main () ????
//3 - value type - local struct; on the main () 's stack.
Now: when the application arrival to execute //XX inside main(), everything inside Myclass::swap goes on the stack (I hope). But why? because is it inside main() function or because is it a call function, independently from where it is?
Then: where is allocated Program class??? Who create an instance of Program??
thanks
marcel
19th August 2007, 11:54
It is:
//myclass.cs
MyClass {
public int a;
public void swapl(ref int x, ref int y) {
int temp;
temp = x;
x = y;
y = temp;
}
}
You actually have to specify the variable type even when passing vaue types by reference.
1 - Allocated on the stack of main() because they are value types and you create them in main. Will be dealocated when main exits.
2 - No. For the third time, reference types are allocated on the heap, no matter where you create them. The class will be deallocated by the garbage collector, not necessarily when main exits.
3 - Correct
Now: when the application arrival to execute //XX inside main(), everything inside Myclass::swap goes on the stack (I hope). But why? because is it inside main() function or because is it a call function, independently from where it is?
Because it is inside the swap function. It is like in C++. Do you know C++?
Then: where is allocated Program class??? Who create an instance of Program??
I am not sure of this. I am not sure even if the main class gets instantiated as one would normally do.
But usually, the class that contains the entry point in the application( the Main() function ) should not provide any functionality, meaning that you don't implement in this class things that you will use in other parts of the application. Therefore you won't ever need to instantiate the class manually.
Regards
mickey
20th August 2007, 09:06
Hi, this continue to confuse me:
Yes but depends... Value types go on the class's heap, while ref types get their own heap.
In the last post you said "value type" goes on the stack; here above no. value type goes on the stack or on the class' heap ? (I guess in this case we're speaking of eg Int32 declared inside a class eg int a ot int temp in my last example). In this wai I understand thta only value types that go on the stack are those are in the main() function and not else where; is it so?
marcel
20th August 2007, 19:40
Whatever... I say to skip things you don't understand, and you'll get them along the way.
Yes but depends... Value types go on the class's heap, while ref types get their own heap.
I was referring strictly to member objects.
I suggest taking C++ and C#, put them side by side and compare them. The basic concepts are almost the same.
Regards
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.