std::basic_string 每次都会破坏空终止吗?
Will std::basic_string destroy null termination every time?
最近,我将我的编译器从 gcc-4.3.x 更新到 gcc-7.x,然后我的一位测试人员遇到了 Assert 异常。
代码如下:
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-terminator。但是我不明白为什么它每次都调用数据的析构方法。有谁能给我答案吗?
谢谢!
GCC 5 已更改 std::basic_string
以符合 C++11 中引入的几个新要求。
您注意到的变化是 "copy on write" 在 GCC 5 中因 std::basic_string
而被放弃。也就是说,同一字符串的副本必须不同,而不仅仅是引用计数。
最近,我将我的编译器从 gcc-4.3.x 更新到 gcc-7.x,然后我的一位测试人员遇到了 Assert 异常。
代码如下:
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-terminator。但是我不明白为什么它每次都调用数据的析构方法。有谁能给我答案吗?
谢谢!
GCC 5 已更改 std::basic_string
以符合 C++11 中引入的几个新要求。
您注意到的变化是 "copy on write" 在 GCC 5 中因 std::basic_string
而被放弃。也就是说,同一字符串的副本必须不同,而不仅仅是引用计数。