程序运行时,内存到底是如何分配的?堆和栈到底有什么区别?

benbebnmao 2002-09-09 05:22:13
感觉糊涂得很,希望多多指点.

1.程序运行时,内存到底是如何分配的?
2.活动记录里到底记了些什么东东?
3.程序指令放在什么地方,数据又放在什么地方?
4.堆和栈到底有什么区别?
int a;
int *p1;
void f(int b)
{
int c;
static d;
int* p2;
......
}

以上变量那些在堆中,那些在栈中?
栈和堆在内存中的位置是怎样的?谁在高地址,谁在低地址
向那个方向增长?
盼望您的答复!
...全文
583 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sandrowjw 2002-09-11
  • 打赏
  • 举报
回复
思路清晰,学习。
krerix 2002-09-11
  • 打赏
  • 举报
回复
收藏了。解释的很清楚。
Last_Dodo 2002-09-10
  • 打赏
  • 举报
回复
To benbebnmao(苯笨猫):Thanks! I take it as compliment because I typed it last night.

You said:
低地址 | data |
|------|
|text |
|------|
|heap |
| |
|------|
| |
|stack |
高地址 |------|

其中,heap事向高地址增长的,stack是向低地址增长的。

It is not necessary true. I have seen something like this and also seen something completely different. It is better to say that the memory layout of a process is depended on compiler/loader and/or OS.

全局变量和静态变量,无论在何处声明的静态变量,都在data中?Yes.

每调用一次函数,都要以这个函数的activation record 压栈,放些返回地址,返回值,参数,还有此函数中声明的局部变量。
heap用来放需动态申请内存的指针。

是不是这样??
Mostly right. Just to clarify one point, that is heap is for the dynamically allocated memory. This memory should be pointed by a pointer type variable. The pointer variable itself will not be on heap. So, "heap用来放需动态申请内存的指针" is not very clear because the pointer variable will not be on heap but the memory it points to could (but not have to).

如果
int f(int *p1)
{
static int *p2=new int;

}
这样,p1,p2放在哪里啊?

The memory p2 pointed to is on heap. "p2" itself is on data segment. "p1" itself is on stack, the memory it points to depending on the actual value passed in. For example:

int *ip1 = new int;
int j;
int *ip2 = &j;
f(ip1); //The memory pointed by the "p1" of f() is the same memory pointed by ip that is on heap. "p1" value is a copy of ip1. This also illustrated that pass pointer is pass by value.
f(&j); //The memory pointed by the "p1" of f() is j which is on stack.
f(ip2); //The memory pointed by the "p1" of f() is j which is on stack.

cwanter 2002-09-10
  • 打赏
  • 举报
回复
http://www.csdn.net/expert/topic/691/691064.xml?temp=.9613611
Last_Dodo 2002-09-10
  • 打赏
  • 举报
回复
Every process has 4 segments, data, text, stack, and heap.

Data segment is for global, static data member, as well as static variable in a function. So, in your example, a, pl, and d are in data segment.

Text segment is for code, all the compile generated (corresponding to your source code) code stored in this are.

Stack is for local variables as well as the activation record of function call. So, c and p2 of your example are in this area. Activation recrod includes at least returning address, return value, and parameter. So, the parameter b of function f is in this area.

Heap is for dynamically allocated memory (many OS put the shared library here as well). In your example, you did not show any code that dynamically allocate memory for pl and p2. If you do, the memory pointer by pl and p2 will be in heap, for example:
pl = new int;
p2 = (int*)malloc(sizeof(int));

It is important to remember that pl and p2 themself are not in heap but the memory they point to might.
honeybe 2002-09-10
  • 打赏
  • 举报
回复
楼上讲的比较形象,好好想想
eastsun 2002-09-10
  • 打赏
  • 举报
回复


打个比方,堆就是一堆沙子,你要用的时候,随便从里面取一些,用后随便归还。而堆栈呢,是你增加和减少的一个叠在一起的一叠碗,你要增加碗,只能放在最上面,你取走碗,也只能从上面开始取,你不能随便从中间抽取。

程序运行的时候,仅指函数(也不讨论98系与NT系对于内存管理的差别)。
如果函数中出现“ int a = 8;”这种语句,这是需要在栈中分配一个int大小的内存出来。如果用到了GlobalAlloc就是要向全局申请内存(范围指:在98中全局就是全计算机内存,在NT中就是本进程空间所占领的内存空间)。用后要释放,用GlobalFree。
liubear 2002-09-10
  • 打赏
  • 举报
回复
简单的说,堆中存放的是全局数据,只有当程序返回,才会释放。
栈中存放的是局部数据,当所定义的函数返回时,其局部变量都被释放
benbebnmao 2002-09-10
  • 打赏
  • 举报
回复
多谢各位,能不能有更详细的解?
还是胡涂
benbebnmao 2002-09-10
  • 打赏
  • 举报
回复
多谢各位!
To: do_do,
你的这段e文从哪考的啊,讲得不错,看意思应该还有一个例子?
有些明白了:)

低地址 | data |
|------|
|text |
|------|
|heap |
| |
|------|
| |
|stack |
高地址 |------|

其中,heap事向高地址增长的,stack是向低地址增长的。全局变量和静态变量,无论在何处声明的静态变量,都在data中?
每调用一次函数,都要以这个函数的activation record 压栈,放些返回地址,返回值,参数,还有此函数中声明的局部变量。
heap用来放需动态申请内存的指针。

是不是这样??

如果
int f(int *p1)
{
static int *p2=new int;

}
这样,p1,p2放在哪里啊?
cwanter 2002-09-09
  • 打赏
  • 举报
回复
一个进程可用内存空间为4G,可分在存放静态数据,代码,系统内存,堆,栈等。.活动记录一般存放调用参数、返回地址等内容。堆和栈最大的区别在于堆是由低地址向高地址分配内存,而栈是由高向低。全局和静态数据存放在全局数据区,其余的在栈中,用malloc 或 new 分配的内存位于堆中。一般来说栈在低地址,堆位于高地址。
wolfboy 2002-09-09
  • 打赏
  • 举报
回复
栈就是你不用删除它,堆就要delete或free
blh 2002-09-09
  • 打赏
  • 举报
回复
1。内存是右操作系统分配的,系统根据程序的要求,静态分配特定的内存区域给该程序执行,同时提供程序动态内存获取的支持,具体内容找一些操作系统的书看看
2。堆用来存放程序通过malloc 或 new 动态分配的内存空间,
栈则是用于保证程序正常执行需要的内存区域,函数调用中的形参、局部变量都放在这里
kenryHuang 2002-09-09
  • 打赏
  • 举报
回复
栈一般指局部变量,是由系统自动分配,离开作用域后自动清理的。。
而堆是自由存储区,你可以自己申请内存,使用,但是你必须自己负责释放
存储。否则会产生内存泄露。

程序运行时的内存分配?这个操作系统有关系
在windows底下,为每个进程分配了4GB的私有空间,而具体的使用情况
nt和98下又有不同。这个讲起来比较麻烦。
请参考Advanced Windows 一书。


程序指令在代码段里,数据在数据段里。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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