简体   繁体   English

堆栈内存仅用于指针,而堆仅用于对象吗?

[英]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 ,则因为intSystem.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'. ...在堆栈上创建一个指针,实际对象存储在称为“堆”的另一种类型的存储位置中。

By this logic, isn't everything stored on the heap and only pointers stored on the stack? 按照这种逻辑,不是所有内容都存储在堆中,而指针仅存储在堆栈中吗? Examples being new instances of System.String , System.Int64 , System.Boolean , System.Decimal etc. 示例是System.StringSystem.Int64System.BooleanSystem.Decimal等的新实例。

I thought I understood it, however clearly I don't, so I would appreciate someone explaining whether the stack is only for pointers or which part I've misinterpreted. 我以为我理解了它,但是我显然不明白,所以我会感谢有人解释堆栈是仅用于指针还是我误解了哪一部分。 Thanks in advance. 提前致谢。

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: 可能的位置是:

  1. For local variables it's a stack. 对于局部变量,它是一个堆栈。 Note that physically values can be stored in CPU registers rather than in stack. 请注意,物理值可以存储在CPU寄存器中,而不是堆栈中。
  2. For class fields it's inside of contiguous chunk of memory allocated in heap for an instance of the class. 对于类字段,它位于堆中为该类实例分配的连续内存中。
  3. For static fields it's allocated in loader heap as a part of type metadata (сorrect me if I am wrong). 对于静态字段,它作为类型元数据的一部分分配在加载程序堆中(如果我错了,请纠正我)。

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.

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