内存分配的问题?

RogerWT 2004-04-24 10:37:26
请问在生成对象时实在内存的什么位置分配空间呢?
new,分配实在堆栈中分配吗?堆栈是个什么概念?比如堆和栈是不同的地址空间还是堆栈是一个意思?递归时要用到堆栈,此堆栈是内存中的什么区域,和cpu的堆栈段有什么关系?
有什么书或文章介绍这方面的知识呢?我找了几本书都说的不清不楚?
请高手指引?
急需答案?
...全文
36 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fansy007 2004-04-25
  • 打赏
  • 举报
回复
堆就是动态内存,用new可以申请一块动态内存,用new在堆中申请的内存要用delete销毁,否则就会造成内存泄漏

栈在函数调用时,存放函数返回地址,形参,局部变量等等,程序运行到其作用域外就释放





darkstar21cn 2004-04-25
  • 打赏
  • 举报
回复
首先明确一下概念:
堆是由系统管理的;
而栈是系统分配给进程、由进程自己管理的,进程结束后,系统自动收回,并且会把它标识为free。
而所谓的堆栈一般说的都是栈,这是国内教材在误人子弟。
new和C中的malloc都是在堆中分配内存空间,如果你不主动释放它们,系统会一直认为那快内存区域一直在使用当中,即使你的程序已经退出。一块已经不用的内存,不被系统标识为free,将不会被再次分配(即使是同样的程序再进行起来也是),这就是所谓的内存泄漏了。
不知道说清楚了没。
cgsw12345 2004-04-25
  • 打赏
  • 举报
回复
EC上说的很清楚:
Const Data:
The const data area stores string literals and other data whose values are known at compile-time. No objects of class type can exist in this area.

All data in this area is available during the entire lifetime of the program. Further, all this data is read-only, and the results of trying to modify it are undefined. This is in part because even the underlying storage format is subject to arbitrary optimization by the implementation. For example, a particular compiler may choose to store string literals in overlapping objects as an optional optimization.

Stack:
The stack stores automatic variables. Objects are constructed immediately at the point of definition and destroyed immediately at the end of the same scope, so there is no opportunity for programmers to directly manipulate allocated but uninitialized stack space (barring willful tampering using explicit destructors and placement new).

Stack:
memory allocation is typically much faster than for dynamic storage (heap or free store) because each stack memory allocation involves only a stack pointer increment rather than more-complex management.

Free Store:
The free store is one of the two dynamic memory areas allocated/freed by new/delete.

Object lifetime can be less than the time the storage is allocated. That is, free store objects can have memory allocated, without being immediately initialized, and they can be destroyed, without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void*, but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

Heap:
The heap is the other dynamic memory area allocated/freed by malloc()/free() and their variants.

Note that while the default global operators new and delete might be implemented in terms of malloc() and free() by a particular compiler, the heap is not the same as free store, and memory allocated in one area cannot be safely deallocated in the other.

Memory allocated from the heap can be used for objects of class type by placement new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

Global/Static:
Global or static variables and objects have their storage allocated at program startup, but may not be initialized until after the program has begun executing. For instance, a static variable in a function is initialized only the first time program execution passes through its definition.

The order of initialization of global variables across translation units is not defined, and special care is needed to manage dependencies between global objects (including class statics). As always, uninitialized proto-objects' storage may be accessed and manipulated through a void*, but no nonstatic members or member functions may be used or referenced outside the object's actual lifetime.

allsword 2004-04-25
  • 打赏
  • 举报
回复
enio(阿新)
编译原理应该有吧
9494
龙书里有原理
allsword 2004-04-25
  • 打赏
  • 举报
回复
函数的调用过程就是栈空间的操作过程。
递归函数-自调用函数(函数体内部直接或间接地自己调用自己)。
新自由呼吸 2004-04-25
  • 打赏
  • 举报
回复
编译原理应该有吧
allsword 2004-04-25
  • 打赏
  • 举报
回复
内存被划分为:代码区code area、全局数据区data code、堆区heap area、栈区stack area.
code area 存放程序的代码
data code 存放程序的全局数据、静态数据和常量
heap area 存放程序的动态数据
stack area 存放程序的局部数据,即各函数中的数据

动态内存,堆(heap)允许程序在运行期间(非编泽时),申请内存空间,这种内存空间随
着程序的运行而时大时小,因此,堆内存是动态的,又称为“动态内存”。
sboom 2004-04-24
  • 打赏
  • 举报
回复
在操作系统分配给本进程的堆栈空间中分配,本进程结束时回收,所以所有变量的定义占用的空间不用我们来释放。堆栈是由系统管理的内存资源。堆栈这个词是一个整体,和数据结构的堆和盏不同。递归时要用到堆栈,此堆栈是操作系统的工作堆栈,操作系统保留一部分特殊的内存空间不分配给进程,留做自己的工作堆栈。如果你知道单片机就知道了,51单片机有32字节的堆栈空间,是用来在递归时临时存放CPU工作变量的地方,所有所有递归的层次受CPU型号的限制。操作系统原理有这方面的知识。我是在实际写单片机程序时才弄明白的。
cngdzhang 2004-04-24
  • 打赏
  • 举报
回复
堆栈就是栈英文是stack,用于支持函数调用时,存放函数返回地址,形参,局部变量
堆就是堆,英文是heap,用于动态分配内存,就是new,malloc的分配了

中文书翻译的不一致而已
freefalcon 2004-04-24
  • 打赏
  • 举报
回复
coastline_4000(爱上小白) 说的有误
楼主还是先看看上面那篇汇总贴吧,里面讨论得差不多了
RogerWT 2004-04-24
  • 打赏
  • 举报
回复
众说纷纭,楼上的解释和另外的解释差别太大,堆栈又成一个概念了。那你说new是在哪里分配空间!
RogerWT 2004-04-24
  • 打赏
  • 举报
回复
错了,谢谢机会,希望有更多的人谈谈看法,理解,
coastline_4000 2004-04-24
  • 打赏
  • 举报
回复
请问在生成对象时实在内存的什么位置分配空间呢?
--- > 不知道, 谁知道会在哪里呢, 你自己开调试器看吧

new,分配实在堆栈中分配吗?
--- > NO!

堆栈是个什么概念?
--- > 堆栈是为了存放局部对象而使用的

比如堆和栈是不同的地址空间还是堆栈是一个意思?
--- > 只有堆栈一个概念

递归时要用到堆栈,此堆栈是内存中的什么区域,和cpu的堆栈段有什么关系?
--- > CPU的堆栈段就是指向内存中的堆栈区域
至于递归的原理解释, 说起来太多了, 你自己看书去

有什么书或文章介绍这方面的知识呢?
--- > 教ASM的书都会介绍这个的
RogerWT 2004-04-24
  • 打赏
  • 举报
回复
我想问的堆栈都是关于内存的,与数据结构没有关系。谢谢机会,我正在拜读高手的解释。
看看大家的理解
jp1984 2004-04-24
  • 打赏
  • 举报
回复
//我说目前我理解的。。
//在程序的运行期间。,编译器不能确定程序所需要内存的数量。所以不能将它包含在语句当中,所以编译器不能预先分配空间。。认识到这个问题运行时系统就采用了堆的概念,你可以把堆简单的理解成是内存的仓库,。。当运用new动态分配空间的时候,实际上编译器向堆申请空间。。堆使程序得到附加的内存资源。。
//至于栈我觉得你说说的应该不是数据结构中的栈吧??CPU的堆栈段好象是一种寻址方式吧??大一 看过一点汇编说过。。反正机理都是一样的。 。好象堆栈段是通过SP寄存器来寻址的,减少了访存的次数。。
bshaozi 2004-04-24
  • 打赏
  • 举报
回复
我也弄不清楚
帮你顶吧
RookieStar 2004-04-24
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2776/2776187.xml?temp=.6984217

牛人们的总结,收益匪浅。。。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧