当前位置:网站首页>动态内存与智能指针
动态内存与智能指针
2022-07-29 00:58:00 【火柴-人】
一、三大内存区域
二、动态内存与智能指针
1、运算符new、delete
2、shared_ptr类

#include<memory>
#include<string>
using namespace std;
int main()
{
shared_ptr<string> p;
//如果p不为空,检查它是否指向一个空string
if (p && p->empty())
{
}
return 0;
}

#include<memory>
#include<string>
#include"test.h"
#include"testClass.h"
using namespace std;
void f(testClass* p)
{
cout << "f" << endl;
delete p;
}
int main()
{
testClass* p;
if (true)
{
shared_ptr<testClass> sp(new testClass("123"),f);
p = sp.get();
}
p->name = "456";//以及释放,不能正确执行
return 0;
}

shared_ptr<string> p1 = make_shared<string>();
auto p2 = make_shared<string>("CDA");3、unique_ptr类






4、weak_ptr

weak_ptr<string> wp(make_shared<string>("123"));
if (auto sp = wp.lock())//判断是否为空
{
}5、使用规范
边栏推荐
- body中基本标签
- 科研环境对人的影响是很大的
- 【搜索】—— 迭代加深/双向DFS/IDA*
- 【Unity项目实践】合成大西瓜
- 560 and K
- golang run时报undefined错误【已解决】
- Cloud native application comprehensive exercise
- 新1688 API 接入说明
- Test / development programmers rely on technology to survive the midlife crisis? Improve your own value
- 我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
猜你喜欢

如何选择专业、安全、高性能的远程控制软件

云原生应用综合练习下

Window object of BOM series

了解网址url的组成后 运用url模块、querystring模块和mime模块完善静态网站

活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!

云原生应用综合练习上

J9 number theory: what factors determine the value of NFT?

Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
![[hcip] two mGRE networks are interconnected through OSPF (ENSP)](/img/fe/8bb51ac48f52d61e8d31af490300bb.png)
[hcip] two mGRE networks are interconnected through OSPF (ENSP)

了解各种路径
随机推荐
Openpyxl border
[search] - iteration deepening / bidirectional dfs/ida*
[WesternCTF2018]shrine
【GoLang】同步锁 Mutex
了解网址url的组成后 运用url模块、querystring模块和mime模块完善静态网站
我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
了解各种路径
What is the ISO assessment? How to do the waiting insurance scheme
Analysis of Multi Chain use cases on moonbeam -- review of Derek's speech in Polkadot decoded 2022
ELS new box falls
【GoLang】网络连接 net.Dial
采用QT进行OpenGL开发(二)绘制立方体
【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
J9 number theory: what factors determine the value of NFT?
Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
560 and K
5g commercial third year: driverless "going up the mountain" and "going to the sea"
新生代公链再攻「不可能三角」
The new generation of public chain attacks the "Impossible Triangle"