程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C/C++中的內存地址分配問題

C/C++中的內存地址分配問題

編輯:C++入門知識

1、程序代碼區:存放函數體的二進制代碼。

2、全局區數據區:全局數據區劃分為三個區域。全局變量和靜態變量的存儲是放在一塊的,初始化的全局變量和靜態變量在一塊區域, 未初始化的全局變量和未初始化的靜態       變量在相鄰的另一塊區域。常量數據存放在另一個區域裡。這些數據在程序結束後由系統釋放。我們所說的BSS段(bss segment)通常是指用來存放程序中未初始化的全局         變量的一塊內存區域。BSS是英文Block Started by Symbol的簡稱。這些內存的分配地址由低到高,與局部變量在棧中分配地址的方向不同,棧中地址的分配有高到低。

3、棧區:由編譯器自動分配釋放,存放函數的參數值,局部變量的值等。其操作方式類似於數據結構中的棧。局部變量在棧中地址的分配有高到低。

4、堆區:一般由程序員分配釋放,若程序員不釋放,程序結束時可能由OS回收。注意它與數據結構中的堆是兩回事,分配方式倒是類似於鏈表,呵呵。

5、命令行參數區:存放命令行參數和環境變量的值。

 

實例:

 

using namespace std; 
 
int p = 1; 
int p1 = 2; 
 
void fun1(){} 
void fun2(){} 
 
int main(int argc, char *argv[]) 
{ 
    const char* s1= "chenyiming"; 
    const char* s2="huangyazhen"; 
    static int  is1; 
    static int  is2; 
    static int  iss1 = 1; 
    static int  iss2 = 2; 
    char * mc = (char *)malloc(sizeof(char)*2); 
    int  * mi = (int *)malloc(sizeof(int)*2); 
    int i,j; 
    double d = 3.2; 
    char *p; 
     
    cout << "*****************代碼區****************" << endl; 
    cout << "fun1() " << (int *)fun1 << endl; 
    cout << "fun2() " << (int *)fun2 << endl; 
    cout << "main() " << (int *)main << endl; 
    cout << "*****************代碼區****************" << endl; 
    cout << endl;  
    cout << "*****************數據區****************" << endl; 
    cout << "****文字常量**************" << endl;  
    cout << "s1 " << (int *)s1 << endl; 
    cout << "s2 " << (int *)s2 << endl; 
    cout << "****未初始化的靜態變量****" << endl; 
    cout << "is1 " << &is1 << endl; 
    cout << "is2 " << &is2 << endl; 
    cout << "****初始化的靜態變量******" << endl; 
    cout << "iss1 " << &iss1 << endl; 
    cout << "iss2 " << &iss2 << endl; 
    cout << "*****************數據區****************" << endl; 
    cout << endl; 
    cout << "******************堆區*****************" << endl; 
    cout << "mc " << (int *)mc << endl; 
    cout << "mi " << (int *)mi << endl; 
    cout << "******************堆區*****************" << endl; 
    cout << endl; 
    cout << "******************棧區*****************" << endl; 
    cout << "&mc " << &mc << endl; 
    cout << "&mi " << &mi << endl; 
    cout << "&i " << &i << endl; 
    cout << "&j " << &j << endl; 
    cout << "&d " << &d << endl; 
    cout << "&p " << &p << endl; 
    cout << "******************棧區*****************" << endl; 
    cout << endl; 
    cout << "**************命令行參數區*************" << endl; 
    cout << "argv[0] " << (int *)argv[0] << endl; 
    cout << "**************命令行參數區*************" << endl; 
    cout << endl;  
}  

#include <iostream>
using namespace std;

int p = 1;
int p1 = 2;

void fun1(){}
void fun2(){}

int main(int argc, char *argv[])
{
 const char* s1= "chenyiming";
 const char* s2="huangyazhen";
 static int  is1;
 static int  is2;
 static int  iss1 = 1;
 static int  iss2 = 2;
 char * mc = (char *)malloc(sizeof(char)*2);
 int  * mi = (int *)malloc(sizeof(int)*2);
 int i,j;
 double d = 3.2;
 char *p;
 
 cout << "*****************代碼區****************" << endl;
 cout << "fun1() " << (int *)fun1 << endl;
 cout << "fun2() " << (int *)fun2 << endl;
 cout << "main() " << (int *)main << endl;
  cout << "*****************代碼區****************" << endl;
 cout << endl;
 cout << "*****************數據區****************" << endl;
 cout << "****文字常量**************" << endl;
 cout << "s1 " << (int *)s1 << endl;
 cout << "s2 " << (int *)s2 << endl;
 cout << "****未初始化的靜態變量****" << endl;
 cout << "is1 " << &is1 << endl;
 cout << "is2 " << &is2 << endl;
 cout << "****初始化的靜態變量******" << endl;
 cout << "iss1 " << &iss1 << endl;
 cout << "iss2 " << &iss2 << endl;
 cout << "*****************數據區****************" << endl;
 cout << endl;
 cout << "******************堆區*****************" << endl;
 cout << "mc " << (int *)mc << endl;
 cout << "mi " << (int *)mi << endl;
 cout << "******************堆區*****************" << endl;
 cout << endl;
 cout << "******************棧區*****************" << endl;
 cout << "&mc " << &mc << endl;
 cout << "&mi " << &mi << endl;
 cout << "&i " << &i << endl;
 cout << "&j " << &j << endl;
 cout << "&d " << &d << endl;
 cout << "&p " << &p << endl;
  cout << "******************棧區*****************" << endl;
 cout << endl;
 cout << "**************命令行參數區*************" << endl;
 cout << "argv[0] " << (int *)argv[0] << endl;
 cout << "**************命令行參數區*************" << endl;
 cout << endl;
}

 

 



 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved