Heap versus Stack allocation implications (.NET)

前端 未结 7 1358
梦如初夏
梦如初夏 2020-12-02 16:03

From an SO answer1 about Heap and Stack, it raised me a question: Why it is important to know where the variables are allocated?

At anoth

7条回答
  •  独厮守ぢ
    2020-12-02 16:49

    (edit: My original answer contained the oversimplification "structs are allocated on the stack" and confused stack-vs-heap and value-vs-reference concerns a bit, because they are coupled in C#.)

    Whether objects live on the stack or not is an implementation detail which is not very important. Jon has already explained this well. When choosing between using a class and struct, it is more important to realize that reference types work differently than value types. Take the following simple class as an example:

    public class Foo
    {
       public int X = 0;
    }
    

    Now consider the following code:

    Foo foo = new Foo();
    Foo foo2 = foo;
    foo2.X = 1;
    

    In this example, foo and foo2 are references to the same object. Setting X on foo2 will also affect foo1. If we change the Foo class to a struct then this is no longer the case. This is because structs are not accessed through references. Assigning foo2 will actually make a copy.

    One of the reasons for putting stuff on the stack is that the garbage collector does not have to clean it up. You typically should not worry about such things; just use classes! Modern garbage collectors do a pretty good job. Some modern virtual machines (like java 1.6) can even determine whether it is safe to allocate objects on the stack even if they are not value types.

提交回复
热议问题