当前位置:网站首页>STL教程9-容器元素深拷贝和浅拷贝问题
STL教程9-容器元素深拷贝和浅拷贝问题
2022-07-03 10:45:00 【贪睡的蜗牛】
浅拷贝问题的一个案例
定义一个类
下面有个指针成员,这样容易出现浅拷贝问题
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;
}
}
};
然后定义一个类的对象,把它放到容器里面
int main() {
const char* a = "name";
Person p(a, 20);
vector<Person>v;
v.push_back(p);
}
然后直接报错
原因在于外面的对象和vector里面对象的name指针都指向了同一块内存,在程序结束后,外面对象开始析构,析构完成后vector里面的元素也要析构, 这样同一块内存就析构了两次,就会发生错误
解决方法
修改后的代码
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;
}
}
};
重新运行没有崩溃
STL容器共性机制(重要)
STL所有容器提供的都是值语意,而非引用语意。容器执行插入元素操作时,内部内部实施了拷贝工作,将我们要插入的元素在拷贝一份放入到容器汇总,而不是将原数据元素直接放进容器中,因此我们提供的元素必须能够被拷贝(如果有指针需要自己去构造)。
最后
这里使用strcpy会出现问题,报错
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 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\源.cpp 13
只需要在头部加上
#define _CRT_SECURE_NO_WARNINGS
边栏推荐
- Key switch: press FN when pressing F1-F12
- Incremental database backup - DB incr DB full
- R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
- Android log system
- Internet socket (non) blocking write/read n bytes
- uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
- Phpcms prompt message page Jump to showmessage
- Bi skills - permission axis
- C语言 AES加解密
- Processes and threads
猜你喜欢
C语言 AES加解密
Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
MATLAB提取不規則txt文件中的數值數據(簡單且實用)
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
软考中级软件设计师该怎么备考
Matlab extracts numerical data from irregular txt files (simple and practical)
Leetcode 46: full arrangement
机器学习 3.2 决策树模型 学习笔记(待补)
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
How should intermediate software designers prepare for the soft test
随机推荐
如何成为一名高级数字 IC 设计工程师(1-3)Verilog 编码语法篇:Verilog 行为级、寄存器传输级、门级(抽象级别)
Web安全总结
One hot code
Tablespace creation management and control file management
Execute kubectl on Tencent cloud container service node
抓包整理外篇fiddler———— 会话栏与过滤器[二]
AMS series - application startup process
Understand go language context in one article
Programmers' entrepreneurial trap: taking private jobs
P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆
VPP three-layer network interconnection configuration
如何成为一名高级数字 IC 设计工程师(1-2)Verilog 编码语法篇:Verilog 1995、2001、2005 标准
How should intermediate software designers prepare for the soft test
软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
DS90UB949
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
2. Hal hardware abstraction layer
. \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
Qt+VTK+OCCT读取IGES/STEP模型
Technical experts from large factories: how can engineers improve their communication skills?