简体   繁体   English

堆栈和堆的内存效果?

[英]Stack and Heap memory effects?

In the following code: 在下面的代码中:

MyClass oMyClass1;
MyClass oMyClass2 = null;

My doubt is how the above two lines will affect memory (stack & heap). 我的疑问是以上两行将如何影响内存(堆栈和堆)。

Will create reference in stack? 将在堆栈中创建引用吗?

my doubt is how the above two lines will affect memory(stack & heap). 我的疑问是以上两行将如何影响内存(堆栈和堆)。

Insufficient information, 2 possibilities: 信息不足,有两种可能性:

  1. they are local variables. 它们是局部变量。 In that case those 2 lines make 2 identical allocations (of a reference, always 32/64 bits) on the stack (whenever and for how long the method is executed). 在那种情况下,那两条线在堆栈上(无论何时执行该方法多长时间)进行2个相同的分配(一个引用,始终为32/64位)。 No allocations on the heap. 堆上没有分配。

  2. they are fields in a class or struct. 它们是类或结构中的字段。 In that case those 2 lines each allocate the size of a reference in the instance when it is created. 在这种情况下,这两行分别在创建实例时为其分配引用的大小。 That instance could be allocated on the stack (when they are struct members) or on the heap (class members). 该实例可以分配在堆栈上(当它们是结构成员时)或在堆上(类成员)。

Strictly speaking, it depends on where the code is. 严格来说,这取决于代码在哪里。

If the code is in a regular function/method, it will affect the stack only. 如果代码在常规函数/方法中,则只会影响堆栈。 There will be no effect on the heap, because no MyClass objects have been constructed yet. 对堆没有影响,因为还没有构造MyClass对象。

Assuming MyClass is a class (ie reference type), the declarations will each reserve enough space on the stack to hold a reference to a MyClass object. 假设MyClass是一个类(即引用类型),则每个声明将在堆栈上保留足够的空间来保存对MyClass对象的引用。

The two declarations are slightly different - the first one has not been initialised, so any attempt to access oMyClass1 before it is set to something will give a compiler error. 这两个声明略有不同-第一个声明尚未初始化,因此在尝试将oMyClass1设置为某种东西之前进行任何尝试都会产生编译器错误。 The second one is initialised, so you will not get a compiler error [although you will get a runtime error if you access a method or property of oMyClass2 before you set it to refer to an actual object, eg with oMyClass2 = new MyClass();]. 第二个是初始化的,因此不会出现编译器错误[尽管在将oMyClass2设置为引用实际对象之前访问oMyClass2的方法或属性,也会出现运行时错误,例如,使用oMyClass2 = new MyClass() ]。

If the code is inside a class declaration: 如果代码在类声明中:

class Fred {
    MyClass oMyClass1;
    MyClass oMyClass2 = null;
}

then it will only be executed during the Fred constructor. 那么它只会在Fred构造函数中执行。 The space (for a Fred object, including space for two MyClass references) will already have been allocated on the heap before the constructor is called. 在调用构造函数之前,空间(对于Fred对象,包括两个MyClass引用的空间)已经在堆上分配了。 The two lines of code will actually have no effect, as the space will already have been initialised to null. 实际上,这两行代码将无效,因为该空间已经被初始化为null。

If it is inside a struct declaration, the effect will be similar, only on the stack (if the struct is local) or global variable memory (if the struct is static). 如果它在结构声明中,则效果仅在堆栈(如果结构是本地)或全局变量内存(如果结构是静态)上是相似的。 [Although, to be fair, I'm a tiny bit unsure where statics get allocated in C# - I am just assuming it is done in a similar way to C++] [尽管,公平地说,我有点不确定在C#中的静态分配位置-我只是假设它是通过与C ++类似的方式完成的]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM