一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 416|回复: 0
收起左侧

[编程题] 如何print main函数地址?自学berkeley操作系统CS162 hw0

[复制链接] |试试Instant~ |关注本帖
harryge00 发表于 2015-5-24 22:32:33 | 显示全部楼层 |阅读模式

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int foo;            /* a staticly allocated variable */

  4. int recur(int i) {      /* a recursive function */
  5.   int j = i;            /* a stack allocated variable within a recursive function */
  6.   printf("recur call %d: stack@ %lx\n", i, j); /* fix this so it prints the address of j */
  7.   if (i > 0) {
  8.     return recur(i-1);
  9.   }
  10.   return 0;
  11. }

  12. int stuff = 7;          /* a statically allocarted, pre-initialized variable */

  13. int main (int argc, char *argv[]) {
  14.   int i;            /* a stack allocated variable */
  15.   char *buf1 = malloc(100); /* dynamically allocate some stuff */
  16.   char *buf2 = malloc(100); /* and some more stuff */
  17.   printf("_main  @ %lx\n", main); /* fix to print address of main */
  18.   printf("recur @ %lx\n", recur); /* fix to print address of recur */
  19.   printf("_main stack: %lx\n", i); /* fix to get address of the stack variable */
  20.   printf("static data: %lx\n", stuff); /* fix to get address of a static variable */
  21.   printf("Heap: malloc 1: %lx\n", (long unsigned int) buf1);
  22.   printf("Heap: malloc 2: %lx\n", (long unsigned int) buf2);
  23.   recur(3);
  24.   return 0;
  25. }
复制代码
注释处要改。直接用printf("_main @ %lx\n", &main);printf("recur @ %lx\n", &recur);?

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-9 00:40

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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