为什么将内存拆分为堆栈和堆? [英] Why is memory split up into stack and heap?

查看:257
本文介绍了为什么将内存拆分为堆栈和堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
什么和堆栈和堆在哪里

关于堆栈还是堆,我有几个问题.

要了解的基本知识是堆栈比堆快,但是受到限制. (如果我错了,请纠正我).

但是,我一直想知道堆栈和堆是如何工作的. RAM只是一块内存,它没有分为堆栈"和堆"(或者是吗?).如果是这样,为什么我们要首先拆分堆栈中的内存并堆放?

OS可以使我们能够分配堆栈中的所有内容->一切进行得更快->世界幸福吗?

我很确定情况并非如此.但为什么!?谁能给我一个深入的答案?

很抱歉,如果该帖子与某人所做的某个帖子重复,那么有太多与堆栈和堆有关的信息,我找不到确切的问题.如果您碰巧知道一个,请继续进行链接.

解决方案

堆栈:堆栈用作一种临时暂存器,供当前正在执行的代码块使用,块称为当前块,而任何块称为该块,依此类推.当前块退出时,它正在使用的局部变量将被忘记.顾名思义,堆栈以后进先出的方式使用.

堆栈最重要的用途之一是跟踪当前的调用链.当一个函数调用另一个函数时,调用者将下一条指令的地址(返回地址)压入堆栈.每个函数退出时,它将调用者的返回地址弹出堆栈,并继续从该地址开始执行代码.它也用于在调用方和被调用方之间传递函数参数和返回值.

:堆是不同的-没有特定的顺序.如果要在代码块中分配内存并使该内存停留在该块末尾之外,请在堆上分配它.当然,您还需要将指针/引用存储在某个地方,以便其他代码可以找到该内存.大多数语言提供的住宿条件.

速度:速度的差异并不是由于内存本身的任何属性引起的-正如您在问题中所说,堆栈和堆通常都占用相同的物理内存.由于堆栈的后进先出性质,在堆栈上分配空间的速度很快:如果将某些东西推入堆栈,则只能在一个地方结束.相反,在堆上分配一个块需要在内存中找到足够大的连续空闲区域.堆栈分配可以像一条指令一样快.堆分配需要调用诸如malloc()之类的内存分配函数.

静态与动态:堆上的内存分配是动态的-是否分配块以及块的大小可以根据程序在运行时收到的输入来确定.必要时甚至可以调整在堆上分配的内存区域的大小.也可以在栈上动态分配内存(参见C标准库函数alloca()),但是一旦当前函数退出,该内存就会丢失.堆栈分配通常是静态的-编译器确定(非寄存器)参数,返回数据和局部变量需要多少空间,并在调用该函数时生成代码以在堆栈上保留必要的空间. /p>

示例:假设您正在创建文字处理器.您无法提前知道文件的大小,甚至无法同时使用多少个文​​件.同时,只要用户希望保持打开状态,就希望用户的文档保留在内存中.如果尝试为堆栈上的文档分配内存,则一次将很难打开多个内存,并且需要创建一个用于创建,编辑,保存和关闭文档的函数.通过在堆上分配空间,您可以创建任意数量的文档,每个文档的大小都适合其包含的数据,并避免将文档的生存期与任何特定函数的生存期相联系.

摘要:简而言之,堆栈保存变量的值(有时使用寄存器值),而堆用于分配将在当前块的生存期之后使用的内存

Possible Duplicate:
What and where are the stack and heap

I have a couple of questions on stack versus heap.

The basic thing to know is that stack is faster than the heap, but is limited. (correct me if I'm wrong).

However, I always wondered how stack and heap work exactly. RAM is just one chunk of memory, it isn't divided into 'stack' and 'heap' (or is it?). If so, why do we split up the memory in stack and heap in the first place?

OS's could just allow us to be able to allocate everything on the stack -> everything goes faster -> happy world?

I'm pretty sure that's not the case. But Why!? Can anyone give me an in-depth answer?

Sorry if this post is a duplicate of some post ever made by some person, there's so many related to stack and heap, I couldn't find the exact question I had. If you happen to know one, go ahead and link it.

解决方案

Stack: The stack is used as a sort of temporary scratch pad for use by the block of code that's currently executing, and whatever block called the current one, and whatever block called that one, and so on. When the current block exits, the local variables it was using are forgotten. As the name indicates, the stack is used in a last in, first out manner.

One of the most important uses of the stack is to keep track of the current call chain. When one function calls another, the caller pushes the address of the next instruction (the return address) onto the stack. As each function exits, it pops it's caller's return address off the stack and continues executing code starting at that address. It's also used for communicating function parameters and return values between caller and callee.

Heap: The heap is different -- there's no particular order to it. If you want to allocate memory in a block of code and have that memory stick around beyond the end of the block, you allocate it on the heap. Of course, you'll also need to store a pointer/reference to it somewhere so that other code can find that memory; most languages provide accomodation that.

Speed: Differences in speed aren't due to any property of the memory itself -- as you say in your question, both stack and heap typically inhabit the same physical memory. Allocating space on the stack is quick due to the stacks LIFO nature: if you push something onto the stack, there's only one place it can end up. By contrast, allocating a block on the heap requires finding a large enough contiguous free region in memory. A stack allocation can be as quick as a single instruction; a heap allocation requires a call to a memory allocation function like malloc().

Static v. dynamic: Allocating memory on the heap is dynamic -- whether to allocate a block, and the size of the block, can be determined according to input the program receives while it's running. Regions of memory allocated on the heap can even be resized if necessary. It's possible to dynamically allocate memory on the stack, too (see the C standard library function alloca()), but that memory will be lost as soon as the current function exits. Stack allocations are usually static -- the compiler determines how much space is needed for the (non-register) parameters, return data, and local variables, and it generates code to reserve the necessary space on the stack when the function is called.

Example: Imagine that you're creating a word processor. You can't know ahead of time how large the document will be, or even how many documents will be in use at the same time. At the same time, you want the user's documents to remain in memory as long as the user wants to keep them open. If you try to allocate memory for the documents on the stack you'll find it difficult to have more than one open at once, and you'll need to create a single function that creates, edits, saves, and closes the document. Allocating the space on the heap allows you to create as many documents as you like, each sized appropriately for the data it contains, and to avoid tying the lifetime of the documents to the lifetime of any particular function.

Summary: In a nutshell, the stack holds the values of variables (sometimes registers are used instead), while the heap is used for allocating memory that will be used beyond the lifetime of the current block.

这篇关于为什么将内存拆分为堆栈和堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆