国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

淺析C/C++變量在內(nèi)存中的分布

2020-02-24 14:27:31
字體:
供稿:網(wǎng)友

在書面測試中經(jīng)常考慮C/C++變量內(nèi)存中的分布,雖然很簡單,但很容易忘記,本文是武林技術(shù)頻道小編帶來的淺析C/C++變量在內(nèi)存中的分布,一起跟著武林技術(shù)頻道小編來了解吧!

先寫一個測試程序:

?

#include <stdio.h>?
#include <malloc.h>?
int g_i = 100;?
int g_j = 200;?
int g_k, g_h;?
int main()?
{?
??? const int MAXN = 100;?
??? int *p = (int*)malloc(MAXN * sizeof(int));?
??? static int s_i = 5;?
??? static int s_j = 10;?
??? static int s_k;?
??? static int s_h;?
??? int i = 5;?
??? int j = 10;?
??? int k = 20;?
??? int f, h;?
??? char *pstr1 = "MoreWindows123456789";?
??? char *pstr2 = "MoreWindows123456789";?
??? char *pstr3 = "Hello";?

?????
??? printf("堆中數(shù)據(jù)地址:0x%08x/n", p);?

??? putchar('/n');?
??? printf("棧中數(shù)據(jù)地址(有初值):0x%08x = %d/n", &i, i);?
??? printf("棧中數(shù)據(jù)地址(有初值):0x%08x = %d/n", &j, j);?
??? printf("棧中數(shù)據(jù)地址(有初值):0x%08x = %d/n", &k, k);?
??? printf("棧中數(shù)據(jù)地址(無初值):0x%08x = %d/n", &f, f);?
??? printf("棧中數(shù)據(jù)地址(無初值):0x%08x = %d/n", &h, h);?

??? putchar('/n');?
??? printf("靜態(tài)數(shù)據(jù)地址(有初值):0x%08x = %d/n", &s_i, s_i);?
??? printf("靜態(tài)數(shù)據(jù)地址(有初值):0x%08x = %d/n", &s_j, s_j);?
??? printf("靜態(tài)數(shù)據(jù)地址(無初值):0x%08x = %d/n", &s_k, s_k);?
??? printf("靜態(tài)數(shù)據(jù)地址(無初值):0x%08x = %d/n", &s_h, s_h);?

??? putchar('/n');?
??? printf("全局?jǐn)?shù)據(jù)地址(有初值):0x%08x = %d/n", &g_i, g_i);?
??? printf("全局?jǐn)?shù)據(jù)地址(有初值):0x%08x = %d/n", &g_j, g_j);?
??? printf("全局?jǐn)?shù)據(jù)地址(無初值):0x%08x = %d/n", &g_k, g_k);?
??? printf("全局?jǐn)?shù)據(jù)地址(無初值):0x%08x = %d/n", &g_h, g_h);?

??? putchar('/n');?
??? printf("字符串常量數(shù)據(jù)地址:0x%08x 指向 0x%08x 內(nèi)容為-%s/n", &pstr1, pstr1, pstr1);?
??? printf("字符串常量數(shù)據(jù)地址:0x%08x 指向 0x%08x 內(nèi)容為-%s/n", &pstr2, pstr2, pstr2);?
??? printf("字符串常量數(shù)據(jù)地址:0x%08x 指向 0x%08x 內(nèi)容為-%s/n", &pstr3, pstr3, pstr3);?
??? free(p);?
??? return 0;?
}?


運行結(jié)果(Release版本,XP系統(tǒng))如下:

?

可以看出:
1. 變量在內(nèi)存地址的分布為:堆-棧-代碼區(qū)-全局靜態(tài)-常量數(shù)據(jù)
2. 同一區(qū)域的各變量按聲明的順序在內(nèi)存的中依次由低到高分配空間(只有未賦值的全局變量是個例外)
3. 全局變量和靜態(tài)變量如果不賦值,默認(rèn)為0。 棧中的變量如果不賦值,則是一個隨機的數(shù)據(jù)。
4. 編譯器會認(rèn)為全局變量和靜態(tài)變量是等同的,已初始化的全局變量和靜態(tài)變量分配在一起,未初始化的全局變量和靜態(tài)變量分配在另一起。

上面程序全在一個主函數(shù)中,下面增加函數(shù)調(diào)用,看看函數(shù)的參數(shù)和函數(shù)中變量會分配在什么地方。

程序如下:

?

#include <stdio.h>?
void fun(int i)?
{?
??? int j = i;?
??? static int s_i = 100;?
??? static int s_j;?

??? printf("子函數(shù)的參數(shù):??????? 0x%p = %d/n", &i, i);?
??? printf("子函數(shù) 棧中數(shù)據(jù)地址: 0x%p = %d/n", &j, j);?
??? printf("子函數(shù) 靜態(tài)數(shù)據(jù)地址(有初值): 0x%p = %d/n", &s_i, s_i);?
??? printf("子函數(shù) 靜態(tài)數(shù)據(jù)地址(無初值): 0x%p = %d/n", &s_j, s_j);?
}?
int main()?
{?
??? int i = 5;?
??? static int s_i = 100;?
??? static int s_j;?

??? printf("主函數(shù) 棧中數(shù)據(jù)地址: 0x%p = %d/n", &i, i);?
??? printf("主函數(shù) 靜態(tài)數(shù)據(jù)地址(有初值): 0x%p = %d/n", &s_i, s_i);?
??? printf("子函數(shù) 靜態(tài)數(shù)據(jù)地址(無初值): 0x%p = %d/n", &s_j, s_j);?
??? putchar('/n');?

??? fun(i);?
??? return 0;?
}?


運行結(jié)果如下:

?

可以看出,主函數(shù)中棧的地址都要高于子函數(shù)中參數(shù)及棧地址,證明了棧的伸展方向是由高地址向低地址擴展的。主函數(shù)和子函數(shù)中靜態(tài)數(shù)據(jù)的地址也是相鄰的,說明程序會將已初始化的全局變量和靜態(tài)變量分配在一起,未初始化的全局變量和靜態(tài)變量分配在一起。

以上就是關(guān)于淺析C/C++變量在內(nèi)存中的分布,你學(xué)會了嗎?如果你還想了解更多的專業(yè)程序知識,建議你來武林技術(shù)頻道進行學(xué)習(xí)吧!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 清流县| 敖汉旗| 巴南区| 祁连县| 南昌市| 洪湖市| 漳浦县| 颍上县| 赤壁市| 高雄县| 寿宁县| 永宁县| 九龙坡区| 葫芦岛市| 类乌齐县| 晋江市| 浦东新区| 策勒县| 棋牌| 广河县| 广西| 民县| 新晃| 黎川县| 莎车县| 儋州市| 柞水县| 松江区| 沂南县| 雅江县| 楚雄市| 临城县| 稷山县| 扎赉特旗| 武山县| 定南县| 囊谦县| 鹿邑县| 班玛县| 剑川县| 东乡族自治县|