#include <iostream>
using namespace std;
//全局变量
int g_a = 10;
int g_b = 10;
//全局常量
const int c_g_a = 10;
const int c_g_b = 10;
int main() {
//局部变量
int a = 10;
int b = 10;
//打印地址
cout << "局部变量a地址为: " << &a << endl;
cout << "局部变量b地址为: " << &b << endl;
cout << "全局变量g_a地址为: " << &g_a << endl;
cout << "全局变量g_b地址为: " << &g_b << endl;
//静态变量
static int s_a = 10;
static int s_b = 10;
cout << "静态变量s_a地址为: " << &s_a << endl;
cout << "静态变量s_b地址为: " << &s_b << endl;
cout << "字符串常量地址为: " << &"hello world" << endl;
cout << "字符串常量地址为: " << &"hello world1" << endl;
cout << "全局常量c_g_a地址为: " << &c_g_a << endl;
cout << "全局常量c_g_b地址为: " << &c_g_b << endl;
const int c_l_a = 10;
const int c_l_b = 10;
cout << "局部常量c_l_a地址为: " << &c_l_a << endl;
cout << "局部常量c_l_b地址为: " << &c_l_b << endl;
return 0;
}
文章来源:https://www.toymoban.com/news/detail-698575.html
可以看出栈的地址是从高地址到地址的。文章来源地址https://www.toymoban.com/news/detail-698575.html
到了这里,关于程序分区:全局区、常量区、栈区、堆区、代码区的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!