堆叠和堆肥是什么?它们实际上位于计算机的内存中在哪里?它们在多大程度上被操作时间或语言控制?它们的范围是什么?它们的大小是什么?它们大小是什么?一个的大小是什么使一个更快?


当前回答

因为有些答案没有被选中, 我要贡献我的米特。

令人惊讶的是,没有人提到,不仅在外来语言(文稿)或平台(英特环)中,而且在纤维、绿线和一些共程实施中,都能找到多个(即与运行的骨层线数无关的)呼叫堆叠。

纤维、绿线和绿线在很多方面都相似,这导致许多混乱。 纤维和绿线之间的区别在于前者使用合作性多任务,而后者可能以合作性或先发制人(甚至两者兼而有之)为特点。

无论如何,两种纤维、绿线和共程的目的都是同时执行多种功能,但并非在单一星级线内平行执行(为区分而见此问题),以有组织的方式相互之间相互转移控制权。

当使用纤维、绿线或共程时,您通常每个函数都有单独的堆叠。 (在技术上,不只是堆叠,而是整个执行环境是每个函数。 最重要的是, cpu 登记册。 ) 对于每一个线串, 每一个线串都有与同时运行的函数一样多的堆叠, 而线串正在根据程序逻辑执行每个函数。 当函数运行到尾端时, 它的堆叠就会被销毁。 因此, 堆叠的数量和寿命都是个王宫 。

注意我所说的“ 通常每个函数有一个单独的堆叠 ” 。 有堆叠和堆叠的 courutines 。 最显著的堆叠 c++ 执行是 推进. coroutine 和 微软 ppl 的 async/ wait 。 (然而, c++ 的可恢复功能 (a. k. a. a. “ 合成和等待 ” ) , 这些功能被建议到 c++/17 , 可能会使用没有堆叠的 comutines 。 )

C++标准图书馆的纤维建议即将提出。 还有一些第三方图书馆。 绿线在Python 和 Ruby 等语言中非常受欢迎。

其他回答

简言之,他们的意思是命令而不是命令!

堆叠:在堆叠项目中, 东西在彼此的顶端, 这意味着要更快和更有效 才能被处理!

所以总是有一个索引来指向特定项目, 处理速度也会更快, 这些项目之间也有关系!

堆肥:没有订单,处理速度会放慢, 价值会混乱在一起,没有具体的订单或索引... 是有随机的,它们之间没有关系... 所以执行和使用时间可以不同...

i 还创建下面的图像, 以显示它们可能看起来如何 :

堆叠 :

堆叠上创建的变量将超出范围,自动交易。 与堆叠上的变量相比, 分配速度要快得多。 使用实际的堆叠数据结构执行。 存储本地数据、 返回地址、 参数通过时使用的方法。 当堆叠过多使用时, 可能会出现堆叠溢出( 大多来自无限或过深的循环, 非常大的配置) 。 堆叠上创建的数据可以在没有指针的情况下使用 。

堆积 :

在 c+++ 中, 堆积上的变量必须手动销毁, 并且绝不会掉出范围 。 数据通过删除、 删除或自由解脱而解脱。 与堆叠上的变量相比, 分配速度较慢。 需要时使用来分配块块数据供程序使用。 当有大量分配和交易位置时, 可能会发生碎裂 。 在 c+++ 或 c 中, 堆积上生成的数据将被指向指针, 并分别与新数据或中位相分配 。

例如:

int foo()
{
  char *pBuffer; //<--nothing allocated yet (excluding the pointer itself, which is allocated here on the stack).
  bool b = true; // Allocated on the stack.
  if(b)
  {
    //Create 500 bytes on the stack
    char buffer[500];

    //Create 500 bytes on the heap
    pBuffer = new char[500];

   }//<-- buffer is deallocated here, pBuffer is not
}//<--- oops there's a memory leak, I should have called delete[] pBuffer;

其他答案只是避免解释静态分配意味着什么。 所以我会解释三种主要分配形式,以及它们通常与下面的堆积、堆叠和数据段的关系。 我还会在 c/c++ 和 python 中展示一些例子,以帮助人们理解。

静态( 静态分配) 变量没有在堆叠上分配。 不要假设- 许多人只是因为“ 静态” 听起来像“ 堆叠 ” 。 它们实际上既不存在于堆叠中, 也不存在于堆叠中。 它们属于所谓的数据段 。

然而,一般而言,最好考虑“范围”和“终生”,而不是“堆积”和“堆积”。

范围指代码中哪些部分可以访问变量。 我们一般认为本地范围(只能通过当前函数访问)与全球范围(任何地方都可以访问)不同,尽管范围可能变得更加复杂。

当一个变量在程序执行期间被分配和交易时, 其使用寿命值是指变量在程序执行期间被分配和交易。 我们通常会想到静态分配( 在整个程序期间会持续不变, 使得它可用于在多个函数调用中存储相同的信息), 而会想到自动分配( 仅在对函数的单次调用中持续不变, 使得它可用于存储仅在您函数期间使用、 一旦完成即可丢弃的信息) 相对于动态

尽管大多数编译者和口译员在使用堆叠、堆堆堆等方面也采取了类似的做法,但只要行为正确,编译者有时会打破这些公约。例如,由于优化,本地变量可能只存在于登记册中,或者完全删除,即使大多数本地变量存在于堆叠中。正如在几个评论中所指出的,你可以自由实施一个甚至不使用堆叠或堆叠的编译者,但有些则可以使用

i 将提供一些简单的附加说明的 c 代码来说明所有这一切。 学习的最佳方式是在调试器下运行一个程序并观看行为。 如果您喜欢阅读 python, 跳到答案的结尾 :

// Statically allocated in the data segment when the program/DLL is first loaded
// Deallocated when the program/DLL exits
// scope - can be accessed from anywhere in the code
int someGlobalVariable;

// Statically allocated in the data segment when the program is first loaded
// Deallocated when the program/DLL exits
// scope - can be accessed from anywhere in this particular code file
static int someStaticVariable;

// "someArgument" is allocated on the stack each time MyFunction is called
// "someArgument" is deallocated when MyFunction returns
// scope - can be accessed only within MyFunction()
void MyFunction(int someArgument) {

    // Statically allocated in the data segment when the program is first loaded
    // Deallocated when the program/DLL exits
    // scope - can be accessed only within MyFunction()
    static int someLocalStaticVariable;

    // Allocated on the stack each time MyFunction is called
    // Deallocated when MyFunction returns
    // scope - can be accessed only within MyFunction()
    int someLocalVariable;

    // A *pointer* is allocated on the stack each time MyFunction is called
    // This pointer is deallocated when MyFunction returns
    // scope - the pointer can be accessed only within MyFunction()
    int* someDynamicVariable;

    // This line causes space for an integer to be allocated in the heap
    // when this line is executed. Note this is not at the beginning of
    // the call to MyFunction(), like the automatic variables
    // scope - only code within MyFunction() can access this space
    // *through this particular variable*.
    // However, if you pass the address somewhere else, that code
    // can access it too
    someDynamicVariable = new int;


    // This line deallocates the space for the integer in the heap.
    // If we did not write it, the memory would be "leaked".
    // Note a fundamental difference between the stack and heap
    // the heap must be managed. The stack is managed for us.
    delete someDynamicVariable;

    // In other cases, instead of deallocating this heap space you
    // might store the address somewhere more permanent to use later.
    // Some languages even take care of deallocation for you... but
    // always it needs to be taken care of at runtime by some mechanism.

    // When the function returns, someArgument, someLocalVariable
    // and the pointer someDynamicVariable are deallocated.
    // The space pointed to by someDynamicVariable was already
    // deallocated prior to returning.
    return;
}

// Note that someGlobalVariable, someStaticVariable and
// someLocalStaticVariable continue to exist, and are not
// deallocated until the program exits.

一个特别令人印象深刻的例子说明为什么区分寿命和范围很重要,那就是变量可以具有局部范围,但固定寿命——例如,在上文的代码样本中“某些局部可变性”。这些变量可以使我们共同但非正式的命名习惯非常混乱。例如,我们说“本地”通常是指“局部范围自动分配变量”,而我们说“全球范围”通常是指“全球范围静态分配变量”。 不幸的是,当我们说“本地”时,我们通常是指“全球范围的静态分配变量”。

C/c+++中的一些语法选择加剧了这一问题,例如许多人认为全球变数不是“静态”的,

int var1; // Has global scope and static allocation
static int var2; // Has file scope and static allocation

int main() {return 0;}

请注意, 在以上声明中加上关键词“ 静态” 会使 var2 无法具有全球范围。 然而, 全球 val1 具有静态分布 。 这不是直观的 。 因此, 我试图在描述范围时永远不要使用“静态” 一词, 而不是说“ 文件” 或“ 文件有限” 的范围。 但是许多人使用“静态” 或“ 静态范围” 来描述一个变量, 只能从一个代码文件中访问 。 在生命周期中, “ 静态” 总是意指从一个代码文件中访问的变量 。

有些人认为这些概念是c/c++/ 具体化的。 它们不是。 例如,下面的python样本说明了所有三种分配类型(在解释语言方面可能存在一些微妙的差异,我不会进入这里)。

from datetime import datetime

class Animal:
    _FavoriteFood = 'Undefined' # _FavoriteFood is statically allocated

    def PetAnimal(self):
        curTime = datetime.time(datetime.now()) # curTime is automatically allocatedion
        print("Thank you for petting me. But it's " + str(curTime) + ", you should feed me. My favorite food is " + self._FavoriteFood)

class Cat(Animal):
    _FavoriteFood = 'tuna' # Note since we override, Cat class has its own statically allocated _FavoriteFood variable, different from Animal's

class Dog(Animal):
    _FavoriteFood = 'steak' # Likewise, the Dog class gets its own static variable. Important to note - this one static variable is shared among all instances of Dog, hence it is not dynamic!


if __name__ == "__main__":
    whiskers = Cat() # Dynamically allocated
    fido = Dog() # Dynamically allocated
    rinTinTin = Dog() # Dynamically allocated

    whiskers.PetAnimal()
    fido.PetAnimal()
    rinTinTin.PetAnimal()

    Dog._FavoriteFood = 'milkbones'
    whiskers.PetAnimal()
    fido.PetAnimal()
    rinTinTin.PetAnimal()

# Output is:
# Thank you for petting me. But it's 13:05:02.255000, you should feed me. My favorite food is tuna
# Thank you for petting me. But it's 13:05:02.255000, you should feed me. My favorite food is steak
# Thank you for petting me. But it's 13:05:02.255000, you should feed me. My favorite food is steak
# Thank you for petting me. But it's 13:05:02.255000, you should feed me. My favorite food is tuna
# Thank you for petting me. But it's 13:05:02.255000, you should feed me. My favorite food is milkbones
# Thank you for petting me. But it's 13:05:02.256000, you should feed me. My favorite food is milkbones

什么是堆叠?

堆叠是一堆物体, 通常是排列整齐的物体。

计算架构中的堆栈是内存区域, 数据以最先出错的方式被添加或删除。 在多行应用程序中, 每串线索将有自己的堆栈 。

什么是堆积物?

堆积成堆的堆积物, 堆积成堆。

在计算结构中,堆积是一个动态分配的内存区域,由操作系统或内存管理库自动管理。 堆积上的内存在程序执行期间被分配、 分配和定期调整, 这可能导致一个叫做碎裂的问题。 当内存物体在小空格中被分配时, 碎片就会发生, 这些空格太小, 无法持有额外的内存对象。 净结果为堆积垃圾邮件的百分比 。

两者加在一起

在一个多行应用程序中, 每串线索都有自己的堆叠。 但是, 所有不同的线条都会共享堆积。 因为不同的线条在一个多行应用程序中共享堆积, 这还意味着线条之间必须有一些协调, 以便它们不会试图同时访问和操作堆积中的同一块内存 。

这是更快的--堆叠还是堆叠?为什么?

堆栈比堆叠要快得多。 这是因为堆叠上的内存分配方式。 堆叠上的内存分配和堆叠指针向上移动一样简单 。

对于新到编程的人来说,使用堆叠可能是一个好主意,因为堆叠比较容易。 因为堆叠是小的, 当你确切知道数据需要多少内存, 或者知道数据大小非常小时, 你会想使用它。 当你知道数据需要大量内存的时候, 使用堆叠比较好, 或者你不确定你需要多少内存( 如动态阵列 ) 。

Java记忆模型

堆栈是存储本地变量(包括方法参数)的内存区域。当涉及到对象变量时,这些变量只是堆积中实际对象的引用(指针)。每当一个对象被即时化,就会留出一块堆积内存以保持该对象的数据(状态)。由于对象可以包含其他对象,有些数据实际上可以保留这些嵌套对象的引用。

堆栈是作为执行线条的抓抓空间预留的内存。当函数被调用时,在堆栈顶部为本地变量和一些簿记数据预留一个区块。当该函数返回时,块就会被未使用,下次调用函数时就可以使用。堆栈总是保留在利弗(最先出)顺序中;最近保留的区块总是下一个要解开的区块。这样就可以很容易地跟踪堆叠; fr 。

堆积是用于动态分配的内存。 与堆叠不同, 堆积区块的分配和分配没有强制模式; 您可以随时分配块块, 并随时释放它。 这使得跟踪堆积中哪些部分在任何特定时间分配或免费更为复杂; 许多定制的堆积分配器可用于调和不同使用模式的堆积性能 。

每一串线索都有堆叠, 而通常应用程序只有一堆(尽管对于不同种类的分配来说, 多堆堆并不罕见) 。

直接回答你的问题:

在多大程度上它们受国家或语言运行时间控制?

当线索创建时, os 分配每个系统级线索的堆叠。 通常, os 被语言运行时间调用来分配应用程序的堆叠 。

其范围是什么?

堆栈附在线条上,因此当线条退出时,堆栈被回收。堆积通常在运行时在应用程序启动时分配,在应用程序(技术处理)退出时再回收。

是什么决定了每个孩子的大小?

当创建线索时,会设定堆栈的大小。 程序启动时会设定堆积的大小, 但随着空间需要, 可能会增长( 分配器需要操作系统的更多内存 ) 。

是什么让一个更快?

堆叠速度更快, 因为访问模式使得从堆叠中分配和处理内存( 指针/ 内插器只是递增或衰减) 变得微不足道, 而堆叠在分配或交易位置上有复杂得多的簿记。 另外, 堆叠中的每个字节往往会非常频繁地被再利用, 这意味着它往往被映射到处理器的缓存处, 从而非常快。 堆积的另一个性能冲击是堆积, 主要是全球资源, 通常是 h 。

清晰的演示:图像来源: vikashazrati.wordpress.com