gpt4 book ai didi

c++ - std::basic_string 每次都会破坏空终止吗?

转载 作者:行者123 更新时间:2023-12-01 14:20:22 29 4
gpt4 key购买 nike

最近,我将我的编译器从 gcc-4.3.x 更新到 gcc-7.x,然后我的一位测试人员遇到了断言异常。

代码如下:

struct data {
data() : _c(0) { ++CREATED; std::cout<<"data CREATED +1"<<_c<<" addres: "<<&_c<<std::endl;}
data(char c) : _c(c) { ++CREATED; std::cout<<"data with C CREATED +1"<<_c<<std::endl;}
data(const data& rhs) : _c(rhs._c) { ++COPIED; }
~data() { ++DESTROYED; std::cout<<"data DESTROYED +1"<<_c<<std::endl;}

char _c;

static size_t CREATED;
static size_t COPIED;
static size_t DESTROYED;
};

size_t data::CREATED = 0;
size_t data::COPIED = 0;
size_t data::DESTROYED = 0;

void testStringReferenceCopiable() {
typedef std::basic_string<data> data_str;
std::cout<<"1"<<std::endl;
data d[] = {'a', 'b', 'c'};
std::cout<<"2"<<std::endl;
data_str s( &d[0], 3 );
std::cout<<"3"<<std::endl;
data_str s2 = s;
std::cout<<"4"<<std::endl;
data_str s3;
std::cout<<"5"<<std::endl;
s3 = s;
}

对于 gcc7.x 输出如下:

1
data(char c) CREATED +1a
data(char c) CREATED +1b
data(char c) CREATED +1c
2
data() CREATED +1
~data() DESTROYED +1
3
data() CREATED +1
~data() DESTROYED +1
4
data() CREATED +1
~data() DESTROYED +1
5
data() CREATED +1
~data() DESTROYED +1
~data() DESTROYED +1c
~data() DESTROYED +1b
~data() DESTROYED +1a

对于 gcc4.3.x 版本,输出是:

data  CREATED +1
1
data with C CREATED +1a
data with C CREATED +1b
data with C CREATED +1c
2
3
4
5
data DESTROYED +1c
data DESTROYED +1b
data DESTROYED +1a

data DESTROYED +1


基本上我能理解为什么它每次都调用 Construct of data() ,可能是因为 null 终止符。但是我不明白为什么它每次都调用数据的析构方法。谁能给我一个答案?

谢谢!

最佳答案

GCC 5 更改了 std::basic_string 以符合 C++11 中引入的几个新要求。

您注意到的变化是 GCC 5 中 std::basic_string 放弃了“写时复制”。也就是说,同一字符串的拷贝必须不同,而不仅仅是引用-计数。

关于c++ - std::basic_string 每次都会破坏空终止吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61069534/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com