1.内存分区模型
2.程序运行前:
//放在主函数外面就是全局变量 //全局变量 int g_a = 10; int g_b = 10; //全局常量 //const表示常量 const int c_g_a = 10; const int c_g_b = 10; int main() { //局部变量 int a = 10; int b = 10; //打印地址 cout << "局部变量a地址为: " << (int)&a << endl; cout << "局部变量b地址为: " << (int)&b << endl; cout << "全局变量g_a地址为: " << (int)&g_a << endl; cout << "全局变量g_b地址为: " << (int)&g_b << endl; //静态变量 //在数据前面加上static就转化为了静态变量 static int s_a = 10; static int s_b = 10; cout << "静态变量s_a地址为: " << (int)&s_a << endl; cout << "静态变量s_b地址为: " << (int)&s_b << endl; cout << "字符串常量地址为: " << (int)&"hello world" << endl; cout << "字符串常量地址为: " << (int)&"hello world1" << endl; cout << "全局常量c_g_a地址为: " << (int)&c_g_a << endl; cout << "全局常量c_g_b地址为: " << (int)&c_g_b << endl; const int c_l_a = 10; const int c_l_b = 10; cout << "局部常量c_l_a地址为: " << (int)&c_l_a << endl; cout << "局部常量c_l_b地址为: " << (int)&c_l_b << endl; system("pause"); return 0; }
3.程序运行后
#include<iostream> using namespace std; int* func() { int a = 10; return &a; } int main() { int* p = func(); cout << *p << endl; return 0; }
4.new操作符
#include<iostream> using namespace std; int* func() { int* a = new int(10); return a; } int main() { int* p = func(); cout << *p << endl; cout << *p << endl; delete p; system("pause"); return 0;