QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 596|回复: 2

透过程序看运行时环境,大家讨论。

[复制链接]
发表于 2004-12-21 15:43:07 | 显示全部楼层 |阅读模式
[code:1]
/*test.c ,by richard*/

void f();
void g();
void h();
void h_1();
void h_2();

int globle=3;

int main(void)
{
/*I DO this ,wanting to know which one of malloc and sbrk do his job
  in the data segment.The man page says that it's the sbrk().and malloc
   in the heap segment.
   */
printf("addr of globle:%x \n\n",&globle);
printf("main:%x f:%x g:%x\n\n"main,f,g);
printf("here you will see that the j,which is not valued,get the value of
i,which is in the previous call\n");
f();
g();
printf("\n sbrk(0):%x\n",sbrk(0));
printf("\n malloc(0):%x\n\n",malloc(0));

printf("now see how the stack increase\n ");
h();
printf("now see how the heap change\n");
/*which one do his job in the heap segment,malloc or sbrk() and why the returned value by malloc changed like that?*/
printf("malloc(2):%x malloc(2):%x\n",malloc(2),malloc(2));
printf("malloc(sizeof(char))twice:%x%x\n",malloc(sizeof(char)),malloc(sizeof(char)));
printf("sbrk(0):%x sbrk(2);%x sbrk(2):%x\n",sbrk(0),sbrk(2),sbrk(2));
}

void g(){
int j;//not valued
printf("j:%d  addr of j:%x",j,&j);
}

void h(){
int k;//not valued
printf(" addr of k:%x",&k);
h_1();
}

void h_1(){
int k;//not valued
printf(" addr of k:%x",&k);
h_2();
}

void h_2(){
int k;//not valued
printf(" addr of k:%x",&k);
}

[/code:1]
/*大家讨论一下运行结果。本来我想看一下堆栈的上下限,弄个堆栈益处的,单没有把握住运行时内存,大家一起讨论一下*/
发表于 2004-12-21 18:48:56 | 显示全部楼层
你最好对代码做一下排版,否则看起来累人。
回复

使用道具 举报

 楼主| 发表于 2004-12-21 19:33:14 | 显示全部楼层
谢了,以后注意。怎么没人啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-6 21:24 , Processed in 0.066841 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表