c++ 基础知识-全局区 1.程序的内存模型-内存四区-全局区 #include <iostream> #include <string> using namespace std; //全局变量 int g_a = 10; //全局常量 const int c_g_a = 15; int main() { //局部变量 int a = 20; //局部常量 const int c_a = 54; //静态常量 static int s_a = 16; //字符 string str_b = "aaaaaa"; //地址 cout<<(int)&g_a<<endl; cout<<(int)&c_g_a<<endl; cout<<(int)&c_a<<endl; cout<<(int)&a<<endl; cout<<(int)&s_a<<endl; cout<<(int)&str_b<<endl; return 0; //4620292 全局变量 //4614144 全局常量 //16317152 局部变量 //16317164 局部常量 //4620296 静态常量 //16317112 字面符常量 //请按任意键继续. . . } 2.程序的内存模型-内存四区-栈区-堆区 #include <iostream> #include <string> using namespace std; int * fun() { //int b = 10;//栈区,用完之后程序本身自动释放,不可以多次访问 int* b = new int(10);//堆区,程序结束后自动释放,也可以利用delete手动释放,可以多次访问 return b; } int main() { int * p = fun(); cout<<*p<<endl; //delete p; cout<<*p<<endl; return 0; }