当前位置:网站首页>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
边栏推荐
- 如何成为一名高级数字 IC 设计工程师(1-4)Verilog 编码语法篇:表达式
- After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
- 2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
- Tablespace creation management and control file management
- C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
- Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
- The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
- Analysis of EPS electric steering system
- Incremental database backup - DB incr DB full
- Double linked list of linear list
猜你喜欢

PHP基础

ASP.NET-酒店管理系统

Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable

面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩

Web安全总结

C language AES encryption and decryption

一文搞懂Go语言Context

进程与线程

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

The world's most popular font editor FontCreator tool
随机推荐
Using onvif protocol to operate the device
C language log base zlog basic use
Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials
Phpcms prompt message page Jump showmessage
How should intermediate software designers prepare for the soft test
Dynamic programming (interval DP)
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
Numpy np.max和np.maximum实现relu函数
软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
程序员的创业陷阱:接私活
按键切换:按F1-F12都需要按Fn
C语言二维数组
机器学习 3.2 决策树模型 学习笔记(待补)
Redis things
phpcms 提示信息页面跳转showmessage
C语言日志库zlog基本使用
Numpy np. Max and np Maximum implements the relu function