[英]Will std::string.assign(const char*) or op= ( with const char *) create a copy of the char*?
Digging through the c++ code, it looks like it does a copy, but I cannot be sure. 仔细研究c ++代码,看起来它像是在做一个副本,但是我不确定。 By "copy", I mean something like strcpy is happening internally.
我所说的“复制”是指内部发生了类似strcpy的事情。 And std:string doesn't end up with a pointer to the same char* internally it was passed.
并且std:string最终不会在内部传递指向同一char *的指针。
const char *somestring = ...
std::string str;
str.assign(somestring);
// or
str = somestring; //in my impl, op= calls to assign(const char*)
std::string
will always copy from the array pointed to by a char *
. std::string
将始终从char *
指向的数组中复制。 You can also provide an iterator range in some constructors. 您还可以在某些构造函数中提供迭代器范围。
Copy or reference counting during copies of one std::string
to another depends on the C++ library that you are using. 在将一个
std::string
复制到另一std::string
期间进行的复制或引用计数取决于您使用的C ++库。
The C++ 2011 standard requires std::string to make real copies. C ++ 2011标准需要std :: string才能创建真实副本。 The 2003 standard allowed reference counted copies.
2003版标准允许参考计数副本。
The GNU libstdc++ uses reference counted strings. GNU libstdc ++使用引用计数的字符串。 And for backward compatibility, even the 2011 standard GCC builds with them.
为了向后兼容,甚至使用它们构建了2011标准GCC。 Otherwise it couldn't link to code built with the 2003 standard.
否则,它将无法链接到使用2003标准构建的代码。
There is a preprocessor definition to turn on the new behavior. 有一个预处理器定义可以打开新行为。
See also std::string copy constructor NOT deep in GCC 4.1.2? 另请参见std :: string复制构造函数在GCC 4.1.2中不深入? and Is std::string refcounted in GCC 4.x / C++11?
和std :: string在GCC 4.x / C ++ 11中重新引用了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.