当前位置:网站首页>STL Tutorial 9 deep copy and shallow copy of container elements
STL Tutorial 9 deep copy and shallow copy of container elements
2022-07-03 11:45:00 【Sleepy snail】
A case of shallow copy problem
Define a class
There is a pointer member below , This is prone to shallow copy problems
class Person {
public:
char* name;
int age;
public:
Person(const char * name,int age) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
~Person() {
if (this->name != NULL) {
delete[] this->name;
}
}
};
Then define a class object , Put it in the container
int main() {
const char* a = "name";
Person p(a, 20);
vector<Person>v;
v.push_back(p);
}
Then report the error directly 
The reason lies in the external objects and vector The object inside name The pointers all point to the same memory , At the end of the program , The outer object starts to destruct , After the completion of deconstruction vector The elements inside should also be destructed , In this way, the same memory is destructed twice , There will be errors 
resolvent

The modified code
class Person {
public:
char* name;
int age;
public:
Person(const char * name,int age) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
Person(const Person& p) {
this->name = new char[strlen(p.name) + 1];
strcpy(this->name, p.name);
this->age = p.age;
}
Person& operator=(const Person& p) {
if (this->name != NULL) {
delete[] this->name;
}
this->name = new char[strlen(p.name) + 1];
strcpy(this->name, p.name);
this->age = p.age;
return *this;
}
~Person() {
if (this->name != NULL) {
delete[] this->name;
}
}
};
Rerun without crash 
STL Container commonality mechanism ( important )
STL All containers provide value semantics , Rather than quoting semantics . When the container performs the insert element operation , Copy work is implemented internally , Put a copy of the elements we want to insert into the container for summary , Instead of putting the original data elements directly into the container , Therefore, the elements we provide must be able to be copied ( If there is a pointer, you need to construct it yourself ).
Last
Use here strcpy There will be problems , Report errors
severity Code explain project file That's ok Disable status
error C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. map D:\store\server\leetcode\map\ Source .cpp 13
Just add
#define _CRT_SECURE_NO_WARNINGS
边栏推荐
- How to become a senior digital IC Design Engineer (1-3) Verilog coding syntax: Verilog behavior level, register transfer level, gate level (abstract level)
- 优化接口性能
- R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
- How should intermediate software designers prepare for the soft test
- Phpcms prompt message page Jump to showmessage
- Program process management tool -go Supervisor
- Dynamic programming (interval DP)
- The world's most popular font editor FontCreator tool
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- FL Studio 20 unlimited trial fruit arranger Download
猜你喜欢

错排问题 (抽奖,发邮件)

金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?

机器学习 3.2 决策树模型 学习笔记(待补)

Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.

After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good

Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
![抓包整理外篇fiddler———— 会话栏与过滤器[二]](/img/04/e9cc027d753e7049f273d866eefdce.png)
抓包整理外篇fiddler———— 会话栏与过滤器[二]

Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)

STL教程9-容器元素深拷贝和浅拷贝问题
随机推荐
Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
After setting up ADG, instance 2 cannot start ora-29760: instance_ number parameter not specified
Double linked list of linear list
2022年中南大学夏令营面试经验
POI excel cell wrap
STL教程9-容器元素深拷贝和浅拷贝问题
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
鸿蒙第四次培训
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
《剑指offer 03》数组中重复的数字
The excel table is transferred to word, and the table does not exceed the edge paper range
836. 合并集合(DAY 63)并查集
Excel快速跨表复制粘贴
Nestjs configuration service, configuring cookies and sessions
mysql使用update联表更新的方法
previous permutation lintcode51
Application of high-precision indoor positioning technology in safety management of smart factory
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
How to make others fear you
R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o