首页 > 系统相关 >c++从入门到精通——const分配内存模型

c++从入门到精通——const分配内存模型

时间:2022-11-01 18:35:33浏览次数:42  
标签:10 const cout int c++ Person 分配内存 test


const分配内存模型

对const变量取地址,分配临时内存

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void test() {
const int a = 10;
int *p = (int *)(&a);
cout << "p=" << p << endl;
cout << "a=" << &a << endl;
cout << "p_address=" << &p << endl;
}

int main() {
test();
return EXIT_SUCCESS;
}

c++从入门到精通——const分配内存模型_开发语言

使用普通变量 初始化 const变量

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void test() {
int a = 10;
const int b = a;
int *p = (int *)&b;
*p = 1000;
cout << " b = " << b << endl;
}

int main() {
test();
return EXIT_SUCCESS;
}

c++从入门到精通——const分配内存模型_#define_02

对于自定义数据类型

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;

struct Person {
string m_Name;
int m_Age;
};

//对于自定义数据类型
void test() {
const Person p;
// p.m_Age = 10;
Person *pp = (Person *)&p;
(*pp).m_Name = "Tom";
pp->m_Age = 10;
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}

int main() {
test();
return EXIT_SUCCESS;
}

c++从入门到精通——const分配内存模型_#define_03


标签:10,const,cout,int,c++,Person,分配内存,test
From: https://blog.51cto.com/u_13859040/5814685

相关文章