当前位置:网站首页>13 managing resources by objects
13 managing resources by objects
2022-07-26 10:41:00 【JeffyGao】
Clause - 13 Manage resources with objects
Remember:
1- In order to prevent resource leakage , Please use RAII( Resource acquisition initialization ) object , They take resources in constructors and release resources in destructors .
2- Two are often used RAII class Namely tr1::shared_ptr and auto_ptr. The former is usually the better choice , Because of its copy The behavior is more intuitive . If choose auto_ptr, The copy action makes it ( The copy ) Point to null.
So-called resources Namely , Once you use it , It must be returned to the system in the future .C++ The longest use is dynamic memory allocation , If not released , Will cause memory leaks .
This clause is for the assigned class object .
routine : After using the object delete Its object . When the number of code is large, it is usually encapsulated with functions .
But there's a problem : Sometimes before you can delete it , I met one of them return. This leads to a memory leak .
resolvent :C++ Of “ Destructor automatic calling mechanism ”.
Two commonly used pointers : auto_ptr、tr1::shared_ptr.
When used normally When the destructor is automatically deleted, the form of the two smart pointers is similar . But when using replication behavior to assign null When ,tr1::shared_ptr Obviously, it looks more comfortable .
【 notes 】auto_ptr and tr1::shared_ptr All are done in the destructor delete instead of deletep[]. That means in dynamic allocation array Use on your body auto_ptr or shared_ptr It's a bad idea . In fact, the definition of vector、string It can replace array 了 .
边栏推荐
- [leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
- 少了个分号
- 第8期:云原生—— 大学生职场小白该如何学
- 控制随机抽中几率 [ C# | Random ]
- 第6期:大学生应该选择哪种主流编程语言
- 【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
- Anaconda is used on vscode (the environment has been configured)
- kali 查看ip地址
- 在altium designer中禁用USBJATG
- vscode上使用anaconda(已经配置好环境)
猜你喜欢
抽象工厂及其改进示例
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
在altium designer中禁用USBJATG
RT-Thread 学习笔记(一)---配置RT-Thread开发环境
RT-Thread 学习笔记(五)---编辑、下载、调试程序
[leetcode daily question 2021/4/29]403. Frogs cross the river
SAP ABAP 守护进程的实现方式
Phase 4: one of College Students' vocational skills preparation in advance
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
[leetcode每日一题2021/4/29]403. 青蛙过河
随机推荐
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
剑指Offer(十):矩形覆盖
uniapp使用简单方法signalR(仅用于web调试,无法打包app)
比较器(Comparable与Comparator接口)
鹏哥C语言第四课(3)
hx711 数据波动大的问题
Simple use of json-c Library -- converting JSON files to struct
剑指Offer(二十一):栈的压入、弹出序列
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
多目标优化系列1---NSGA2的非支配排序函数的讲解
STM32 阿里云MQTT esp8266 AT命令
记给esp8266烧录刷固件
剑指Offer(四十三):左旋转字符串
STM32 Alibaba cloud mqtt esp8266 at command
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
关于硕博士开题报告编写的思考
MD5 encryption
Issue 8: cloud native -- how should college students learn in the workplace
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
在altium designer中禁用USBJATG