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


当前回答

当在加载代码和数据设置后创建进程时, 在数据结束和基于架构的地址空间顶端堆叠后, 启动一个进程, 然后在装入代码和数据设置后启动堆放

当需要更多堆积时, os 将会动态分配, 而堆积块总是几乎毗连

请见 brk ()、 sbrk () 和 ALloca () 系统在 Linux 中调用

其他回答

堆叠堆叠

记忆不会成为支离破碎的本地变量, 只能对堆叠大小(依赖的)变量进行限制, 无法调整大小

堆肥

可在全球范围内访问变量,没有内存大小的限制(相对而言)更慢的存取,没有保证有效使用空间的保障,随着分配内存区块,内存可能会随着时间而变得支离破碎,然后释放后,你必须管理内存(你负责分配和释放变量),变量可以使用地环调整大小()

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

它们(在真实的计算机记忆中)在哪里和什么?

回答:两者都是在山上。

暂不考虑 :

公牛就像一张书桌, hdds/sds(永久存储)就像一张书架。 要阅读任何东西, 您必须在桌子上打开一本书, 您只能在桌子上打开尽可能多的书。 要拿到一本书, 您可以从书架上拉出来, 并在桌子上打开。 返回一本书, 您关闭桌子上的书架, 然后把它还给书架 。

堆叠和堆肥是我们给两种方式的汇编者提供的名称,它们将不同种类的数据储存在同一地点(即用大号堆放)。

它们的范围是什么?它们各自的大小由什么决定?一个的大小由什么决定?一个的大小由什么决定?

答复:

堆栈是静态(固定大小) a. 编译器读取您代码中使用的变量类型。i. 它为这些变量分配了固定的内存量。 ii. 此内存的大小无法增长。 b. 内存是毗连的( 单块) , 因此存取有时比堆叠的更快。 放在堆叠上的一个物体在堆叠大小超过其大小的运行时会增加内存, 导致堆叠溢出错误, 堆积是动态( 变化大小) 数据 a. amoun 。

暂不考虑 :

堆叠和堆肥主要不是为了提高速度而引入的;它们被引入是为了处理内存溢出。 有关堆叠与堆积之间的第一个问题应该是:是否会出现内存溢出。 如果一个物体的大小打算增长到一个未知的数量(如链接的清单或其成员可以持有任意数量数据的对象),则将其放入堆肥。 尽可能使用 c++ 标准库(stl) 容器矢量、 地图和列表,因为它们是内存和列表。

在运行您的代码后, 如果您发现代码运行速度慢得令人无法接受, 然后回去重新构思您的代码, 并检查它是否能够更高效地编程。 它可能会发现问题与堆叠或堆积完全无关( 比如使用迭代算法而不是循环算法, 看看 i/ o / vs. cpu- imbound 任务, 也许添加多读或多处理 ) 。

因为程序的速度可能与堆叠或堆叠上分配的物品无关。

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

答复:

堆叠大小由编译者在编译时确定。 堆积大小在运行时会变化。 (堆积在运行时与 os 一起工作,以分配内存 。)

暂不考虑 :

下面是控制和编译时间相对于运行时间操作的多一点。

每台计算机都有独特的指令集结构(isa),这是其硬件命令(例如“移动”、“跳”、“跳”、“添加”等)。

a os 只不过是一个资源管理者(控制如何/时间/和何处使用内存、处理器、装置和信息)。 os 的状态被称为光机,其余的命令被称为扩展机。 内核是扩展机的第一层。 它控制着诸如确定什么任务可以使用处理器(调度器)、多少内存或有多少硬件登记册可以分配给任务(调度器),以及任务的顺序。

虚拟内存中的每个过程的堆叠、堆积和数据 :

我想其他很多人 已经给了你 大部分正确的答案 关于这件事情。

然而,一个被忽略的细节是,“堆积”实际上可能应该被称为“免费商店”。 之所以有这种区别,是因为最初的免费商店是用被称为“二成式堆肥”的数据结构来安装的。 因此,从早期实施中分配的麦洛克()/免费()是从堆肥中分配的。 然而,在现代,大多数免费商店都是用非常精密的数据结构来安装的,而不是二成式堆肥。