淺析C/C++變量在內(nèi)存中的分布
C/C++變量在內(nèi)存中的分布在筆試時(shí)經(jīng)??嫉?,雖然簡單,但也容易忘記,因此在這作個(gè)總結(jié),以加深印象。
先寫一個(gè)測試程序:
#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;
}
運(yùn)行結(jié)果(Release版本,XP系統(tǒng))如下:
可以看出:
1. 變量在內(nèi)存地址的分布為:堆-棧-代碼區(qū)-全局靜態(tài)-常量數(shù)據(jù)
2. 同一區(qū)域的各變量按聲明的順序在內(nèi)存的中依次由低到高分配空間(只有未賦值的全局變量是個(gè)例外)
3. 全局變量和靜態(tài)變量如果不賦值,默認(rèn)為0。 棧中的變量如果不賦值,則是一個(gè)隨機(jī)的數(shù)據(jù)。
4. 編譯器會(huì)認(rèn)為全局變量和靜態(tài)變量是等同的,已初始化的全局變量和靜態(tài)變量分配在一起,未初始化的全局變量和靜態(tài)變量分配在另一起。
上面程序全在一個(gè)主函數(shù)中,下面增加函數(shù)調(diào)用,看看函數(shù)的參數(shù)和函數(shù)中變量會(huì)分配在什么地方。
程序如下:
#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;
}
運(yùn)行結(jié)果如下:
可以看出,主函數(shù)中棧的地址都要高于子函數(shù)中參數(shù)及棧地址,證明了棧的伸展方向是由高地址向低地址擴(kuò)展的。主函數(shù)和子函數(shù)中靜態(tài)數(shù)據(jù)的地址也是相鄰的,說明程序會(huì)將已初始化的全局變量和靜態(tài)變量分配在一起,未初始化的全局變量和靜態(tài)變量分配在一起。
上一篇:利用C語言實(shí)現(xiàn)HashTable
欄 目:C語言
下一篇:字符串中找出連續(xù)最長的數(shù)字字符串的實(shí)例代碼
本文標(biāo)題:淺析C/C++變量在內(nèi)存中的分布
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4148.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解
- 01-10Linux線程管理必備:解析互斥量與條件變量的詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法