隨筆-295  評論-26  文章-1  trackbacks-0
          1.程序段:程序段為程序代碼在內存中的映射.一個程序可以在內存中多有個副本.
          2.初始化過的數據:在程序運行值初已經對變量進行初始化的
          3.未初始化過的數據:在程序運行初未對變量進行初始化的數據
          4.堆(stack):存儲局部,臨時變量,在程序塊開始時自動分配內存,結束時自動釋放內存.存儲函數的返回指針.
          5.棧(heap):存儲動態內存分配,需要程序員手工分配,手工釋放.
          ?

          # include <stdio.h>

          int g1=0, g2=0, g3=0;

          intmax(int i)
          {
          ????int m1=0,m2,m3=0,*p_max;
          ????static n1_max=0,n2_max,n3_max=0;
          ????p_max =(int*)malloc(10);
          ????printf("打印max程序地址\n");
          ????printf("in max: 0x%08x\n\n",max);
          ????printf("打印max傳入參數地址\n");
          ????printf("in max: 0x%08x\n\n",&i);
          ????printf("打印max函數中靜態變量地址\n");
          ????printf("0x%08x\n",&n1_max);//打印各本地變量的內存地址
          ????printf("0x%08x\n",&n2_max);
          ????printf("0x%08x\n\n",&n3_max);
          ????printf("打印max函數中局部變量地址\n");
          ????printf("0x%08x\n",&m1);//打印各本地變量的內存地址
          ????printf("0x%08x\n",&m2);
          ????printf("0x%08x\n\n",&m3);
          ????printf("打印max函數中malloc分配地址\n");
          ????printf("0x%08x\n\n",p_max);//打印各本地變量的內存地址

          ????if(i)return 1;
          ????elsereturn 0;
          }

          int main(int argc,char**argv)
          {
          staticint s1=0, s2, s3=0;
          int v1=0, v2, v3=0;
          int*p;????
          p =(int*)malloc(10);

          printf("打印各全局變量(已初始化)的內存地址\n");
          printf("0x%08x\n",&g1);//打印各全局變量的內存地址
          printf("0x%08x\n",&g2);
          printf("0x%08x\n\n",&g3);
          printf("======================\n");
          printf("打印程序初始程序main地址\n");
          printf("main: 0x%08x\n\n", main);
          printf("打印主參地址\n");
          printf("argv: 0x%08x\n\n",argv);
          printf("打印各靜態變量的內存地址\n");
          printf("0x%08x\n",&s1);//打印各靜態變量的內存地址
          printf("0x%08x\n",&s2);
          printf("0x%08x\n\n",&s3);
          printf("打印各局部變量的內存地址\n");
          printf("0x%08x\n",&v1);//打印各本地變量的內存地址
          printf("0x%08x\n",&v2);
          printf("0x%08x\n\n",&v3);
          printf("打印malloc分配的堆地址\n");
          printf("malloc: 0x%08x\n\n",p);
          printf("======================\n");
          ????max(v1);
          printf("======================\n");
          printf("打印子函數起始地址\n");
          printf("max: 0x%08x\n\n",max);
          return 0;
          }

          ?

          這個程序可以大致查看整個程序在內存中的分配情況:
          可以看出,傳入的參數,局部變量,都是在棧頂分布,隨著子函數的增多而向下增長.
          函數的調用地址(函數運行代碼),全局變量,靜態變量都是在分配內存的低部存在,而malloc分配的堆則存在于這些內存之上,并向上生長



          大盤預測 國富論
          posted on 2009-03-10 15:40 華夢行 閱讀(226) 評論(0)  編輯  收藏 所屬分類: C
          主站蜘蛛池模板: 鄱阳县| 张北县| 新安县| 工布江达县| 西充县| 桃源县| 太谷县| 洪湖市| 天津市| 土默特左旗| 且末县| 行唐县| 长汀县| 孙吴县| 桂东县| 莱芜市| 大悟县| 黄石市| 通海县| 常德市| 长葛市| 富阳市| 女性| 贵德县| 平邑县| 康平县| 萨迦县| 郧西县| 白水县| 射阳县| 普安县| 白山市| 永胜县| 彰武县| 诏安县| 綦江县| 湟源县| 黄大仙区| 巨鹿县| 宿迁市| 福鼎市|