当前位置:网站首页>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 了 .
边栏推荐
- datav漂亮数据屏制作体验
- Uninstall Meizu app store
- .net operation redis set unordered collection
- 剑指Offer(四十四):翻转单词顺序序列
- 扫雷pro版2021-08-19
- C language calculation date interval days
- The problem of large fluctuation of hx711 data
- SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
- 第8期:云原生—— 大学生职场小白该如何学
- json_ object_ put: Assertion `jso->_ ref_ count > 0‘ failed. Aborted (core dumped)
猜你喜欢

Issue 5: the second essential skill for College Students

The problem of large fluctuation of hx711 data

多目标优化系列1---NSGA2的非支配排序函数的讲解

QRcode二维码(C语言)遇到的问题

异常的概念与处理

【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)

$router和$route的区别

uniapp使用简单方法signalR(仅用于web调试,无法打包app)

粽子大战 —— 猜猜谁能赢

Write to esp8266 burning brush firmware
随机推荐
putty的使用教程
第8期:云原生—— 大学生职场小白该如何学
[leetcode daily question 2021/4/29]403. Frogs cross the river
剑指Offer(二十一):栈的压入、弹出序列
Datav beautiful data screen production experience
.net operation redis list list
Oracle create index
Redis docker instance and data structure
在altium designer中禁用USBJATG
12 don't forget every component when copying an object
MD5加密
C语言鹏哥20210812C语言函数
winpcap 抓包函数pcap_loop(),停止问题
Issue 5: the second essential skill for College Students
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】
Some web APIs you don't know
Simple use of json-c Library -- converting JSON files to struct
.net operation redis string string
C language calculation date interval days
第5期:大学生入职必备技能之二