为什么堆内存应运而生? [英] Why did heap memory come into existence?

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

问题描述

我的问题几乎是最基本的,正如标题所述,我想知道为什么堆内存出现了.我知道堆内存支持动态内存分配,但是当两种类型的内存(静态和堆)都存在于RAM上时,为什么不能通过/在堆栈内存中进行相同的分配.为什么需要在进程地址空间中有单独的内存段.在发布此问题时,我遇到了与堆内存主题相关的几篇文章,但到目前为止,我检查并没有回答我的问题,而是更多关于堆内存的定义和行为. 为了更清楚地说明我的问题,为什么不能有一个大型堆栈内存来处理动态内存分配.众所周知,动态内存是动态创建的,可以在需要时释放.是动态内存分配的内存溢出或泄漏问题,但如果有一个大堆栈内存就无法解决,因为如果没有释放内存,那么它是堆还是堆栈',那么即使在程序退出的情况下,也肯定会导致浪费曾经说过,当两个内存都驻留在RAM上时,为什么要使用不同的内存结构来处理内存.

My question is pretty much basic and as the title says I want to know why heap memory came into picture . I am aware that heap memory supports dynamic memory allocation but why same could not be done through/within stack memory when both types of memory (static and heap) exist on RAM. Why it was needed to have separate segment of memory in process address space .While posting this question I came across several posts related to heap memory topic but so far I have checked does not answer my question rather is more about definition and behaviour of heap memory. To be more clear on my question , Why there couldn't be one large stack memory which also handles dynamic memory allocation .As we know that dynamic memory is something which is created on the fly and can be freed up when required.Also there could be memory overrun or leak issue with dynamic memory allocation but can't it be done if there was one big stack memory as be it heap or stack if memory doesnt get freed up 'then it will definitely result in wastage even in case of program exit .Having said that when both memory resides on RAM then why there was requirement of different memory structure for handling memory.

推荐答案

对于堆栈分配,对于已分配对象的生命周期,有严格的LIFO(后进先出)策略.也就是说,如果先分配对象A,然后分配对象B,再分配对象C,则它们将始终以相反的顺序释放.

With stack allocation there is a strict LIFO (last-in, first-out) policy for the lifetimes of allocated objects. That is, if you allocate object A, then object B, then object C, they will always be freed in the opposite order.

在很多情况下这很有用,但在某些情况下,例如希望对象C在B和A被销毁后继续存在.没有办法用堆栈来做到这一点,因此要处理那些引入了堆的情况.正确使用堆栈比分配堆栈难,但也更灵活/强大.

That's useful in a lot of cases, but there are also cases where you e.g. want object C to continue to exist after B and A have been destroyed. There's no way to do that with a stack, so to handle those cases the heap was introduced. It's harder to use correctly than stack allocation, but also more flexible/powerful.

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

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