昨天看书看兴奋了。。。
今早睡不着,六点半起床呀(平时一般七点四十五呀),
按书上测试代码呀,
考察C代码的各种变量在代码区,STACK区,HEAP区,BSS等的存放位置呀。。
XXX,像我这样的人,有木有呀,像我这样的人,有木有呀,
1 [root@localhost ccode]# cat mem_add.c 2 #include <stdio.h> 3 #include <malloc.h> 4 #include <unistd.h> 5 #include <alloca.h> 6 extern void afunc(void); 7 extern etext, edata, end; 8 int bss_var; 9 int data_var = 42; 10 #define SHW_ADR(ID, I) printf("The %8s\t is at address: %8x\n", ID, &I); 11 int main(void) 12 { 13 char *p, *b, *nb; 14 printf("Adrress etex:%8x\t Adr edata %8x\t Adr end %8x\t\n", &etext, &edata, &end); 15 printf("\ntext Location:\n"); 16 SHW_ADR("main", main); 17 SHW_ADR("afunc",afunc); 18 printf("\nbss Location:\n"); 19 SHW_ADR("bss_var", bss_var); 20 printf("\ndata Location:\n"); 21 SHW_ADR("data_var",data_var); 22 printf("\nStack Location:\n"); 23 afunc(); 24 p = (char *)alloca(32); 25 if(p != NULL) 26 { 27 SHW_ADR("start", p); 28 SHW_ADR("end", p + 31); 29 } 30 b = (char *)malloc(32 * sizeof(char)); 31 nb = (char *)malloc(16 * sizeof(char)); 32 printf("\nHeap Location:\n"); 33 printf("\nthe Heap start:%p\n",b); 34 printf("\nthe Heap end:\%p\n",(nb = 16 * sizeof(char))); 35 printf("\nb and nb in Stack\n"); 36 SHW_ADR("b", b); 37 SHW_ADR("nb", nb); 38 free(b); 39 free(nb); 40 return 0; 41 } 42 void afunc(void) 43 { 44 static int long level = 0; 45 int stack_var; 46 if(++level == 5) 47 { 48 return; 49 } 50 printf("stack_var is at:%p\n", &stack_var); 51 afunc(); 52 }
时间: 2024-12-27 20:51:56