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


当前回答

a couple of cents: 我认为, 绘制内存图形比较简单, 将会是件好事 :

箭头 - 显示生长堆叠和堆肥的位置, 进程堆叠大小有限制, 以 os 定义, 以线状线条参数为线状堆叠大小限制通常创建 api 。 通常以进程最大虚拟内存大小为限制, 例如32 位 2-4 gb 。

如此简单的方式:过程堆积对于过程和内部所有线条来说都是一般的, 用于记忆分配, 常见的情况是像 malloc () 。

堆栈是用于存储常见函数返回指针和变量的快速存储存储存储的快速内存, 被处理为函数调用中的参数, 本地函数变量 。

其他回答

其他答案只是避免解释静态分配意味着什么。 所以我会解释三种主要分配形式,以及它们通常与下面的堆积、堆叠和数据段的关系。 我还会在 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

堆栈是内存的一部分,可以通过若干关键组装语言指令来操作,例如“pop”(从堆叠中移动并返回一个值)和“push”(将一个值推到堆叠中),但也可以调用(调用子例程 - 将地址推到堆叠中)和返回( 从子例程返回 - 将堆叠的地址从堆叠中跳出并跳到堆叠中)。这是堆叠指针登记册下方的内存区域,可以根据需要设置。堆叠也用于 pa 。

堆积是操作系统向一个应用程序提供的内存的一部分, 通常通过像马洛克这样的音响。 在现代套管上, 这个内存是一组只有呼叫程序才能进入的页面 。

堆叠的大小在运行时确定, 一般在程序启动后不会增长。 在 c 程序中, 堆叠需要足够大, 以保持每个函数中所有声明的变量。 堆叠会按需要动态增长, 但 巨头最终会发出呼唤( 它会增加的堆积量通常超过 merloc 要求的值, 这样至少有些未来的中枢不需要返回内核来获取更多的内核内存 。 此行为通常可以自定义 )

因为您在启动程序前已经分配了堆叠, 所以在您使用堆叠之前, 您从不需要在使用堆叠前使用滚动, 所以在实际操作中这是一个小的优势。 实际上, 很难预测什么是快速的,什么是缓慢的, 在拥有虚拟内存子系统的现代操作系统中, 因为这些页面是如何被安装的, 在哪里存储的, 是一个执行细节 。

其他人直接回答了您的问题, 但是当试图理解堆叠和堆积时, 我认为考虑传统的 unix 进程( 没有线条和基于 mmap () 的缩放符) 的内存布局( 没有线条和 mmap () 的缩放符 ) 。 内存管理词汇表网页有一张内存布局图 。

堆叠和堆肥传统上位于进程虚拟地址空间的对面。 堆叠在存取时自动增长, 以内核设定的大小( 内核可以使用设置限制( rlimit_ stack,...) 调整) 。 当内存分配器引用 brk () 或 sbrk () 系统调用时, 堆叠会增长, 将更多页的物理内存映入进程的虚拟地址空间 。

在没有虚拟内存的系统中,例如一些嵌入系统,通常适用同样的基本布局,但堆叠和堆积大小固定。然而,在其他嵌入系统中(例如基于微芯片的微控制器),程序堆叠是一个单独的内存块,无法通过数据移动指示处理,只能通过程序流指示(调用、返回等)进行间接修改或阅读。

您可以用堆叠做一些有趣的事情。例如,您有像 Alloca(假设您能够通过关于堆叠使用的大量警告)这样的功能,这是一种形式的中转器,专门用堆叠而不是堆叠来记忆。

也就是说,基于堆叠的内存错误是我经历过的最糟糕的内存错误。 如果您使用堆积内存, 并且越过分配区块的界限, 您有触发断段故障的好机会 。 ( 不是100%: 您的区块可能附带地与先前分配的区块相毗连 。) 但是由于堆叠上创建的变量总是相互连结, 写出边框可以改变另一个变量的值 。 我了解到, 只要我感觉到, 只要我感觉到它, 就会改变另一个变量的值 。

20世纪80年代,Unix像兔子一样传播,大公司自己滚动。 Exxon拥有一个,历史也失去了数十个品牌。 许多执行者都决定如何留下记忆。

典型的 c 程序在记忆中平坦,有机会通过改变 brk () 值来增加。 典型的情况是, 堆积量略低于这个 brk 值, 增加 brk 增加了可用堆积量 。

单堆叠一般是堆积层下的一个区域, 它是一个内存的块块, 在下一个固定的内存区块的顶部之前, 没有任何价值。 下一个块块通常是代码, 在其时代著名的黑客之一的堆叠数据中, 可能被堆叠数据覆盖 。

一个典型的内存区块是 bss (一个零值块) , 在一个制造商的报价中, 意外没有零。 另一个是包含初始值的数据, 包括字符串和数字。 第三个是包含 Crt( cruntime) 、 主机、 函数和图书馆的代码 。

虚拟内存在 unix 中出现 。 许多限制 。 这些区块需要毗连, 或固定大小, 或现在订购特定方式, 没有客观的理由 。 当然, unix 之前的多立方体没有受到这些限制的影响 。 下面是一张图表, 显示这个时代的记忆布局 。