[英]Is stack memory only for pointers and heap for objects?
Firstly, I'm currently working in C# and I've been reading up on memory management. 首先,我目前正在使用C#,并且一直在阅读有关内存管理的内容。 So far, I've read through some great answers on stack overflow explaining the difference between stack memory and the managed heap memory. 到目前为止,我已经阅读了一些有关堆栈溢出的好答案,解释了堆栈内存和托管堆内存之间的区别。 The majority of the answers state that by declaring: 大多数答案通过声明来说明:
int x = 5
, you're allocating enough memory for the type of x
within the stack memory. int x = 5
,您正在堆栈内存中为x
类型分配足够的内存。
I understand how this works as well as the scope of it, however when I read the explanation of heap memory, it confused me. 我了解这是如何工作的以及它的范围,但是当我阅读堆内存的说明时,这让我感到困惑。
If you're saying int x = 5
, since int
is an alias of System.Int32
, wouldn't x
technically be a pointer to a new instance of the System.Int32
struct? 如果您说的是int x = 5
,则因为int
是System.Int32
的别名,因此x
技术上讲不是System.Int32
结构新实例的指针吗? And if so, wouldn't it then be stored in the heap memory since that's used for instance objects. 如果是这样,那么它将不存储在堆内存中,因为它用于实例对象。
In this tutorial, it states (for the line class1 cls1 = new class1()
): 在本教程中,它声明(对于class1 cls1 = new class1()
):
... creates a pointer on the stack and the actual object is stored in a different type of memory location called 'Heap'. ...在堆栈上创建一个指针,实际对象存储在称为“堆”的另一种类型的存储位置中。
System.String
, System.Int64
, System.Boolean
, System.Decimal
etc.
示例是System.String
, System.Int64
, System.Boolean
, System.Decimal
等的新实例。
You can use the following rule: if it's a struct (including primitive types) then it's allocated where it's declared, otherwise a pointer to an object in heap is allocated. 您可以使用以下规则:如果它是一个结构(包括原始类型),则将其分配在声明的位置,否则分配指向堆中对象的指针。
Possible locations are: 可能的位置是:
Warning: this is just basic, non-comprehensive explanation to have a basic understanding of what's going on. 警告:这只是基本的,不全面的解释,对发生的事情有基本的了解。 The reality is more complicated. 现实更加复杂。 Local variables can be hoisted and moved to heap, optimizer can eliminate them altogether, etc... 可以将局部变量提升并移动到堆中,优化器可以完全消除它们,等等。
You may want to check Classes and structs (MSDN) to understand what is stored where and how: 您可能需要检查类和结构(MSDN),以了解什么存储在何处以及如何存储:
int x = 1; // 32 bits holding an integer in the stack
System.Object bo = x; // 32+some more bits are on the heap to hold the "boxed" (wrapped to be kept on the heap) integer value
System.Object ho = new Object(); // some bits are created on the heap right from the start
In simple words there are two types of objects: classes and structs. 简单来说,有两种类型的对象:类和结构。 The classes (reference types) are meant to be stored on the heap and have a pointer to them while structs are meant to be stored in the stack (the structs can be relocated to the heap though with a little overhead of wrapping("boxing") them). 这些类(引用类型)应存储在堆中并具有指向它们的指针,而结构应存储在堆栈中(结构可以重定位到堆,尽管有一些包装开销(“装箱” )他们。
If you really need/want to understand how CLR works in general, consider reading "CLR via C#" (Richter). 如果您真的需要/想要了解CLR的总体工作原理,请考虑阅读“通过C#进行CLR”(Richter)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.