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; }
补充:软件开发 , C++ ,