C++ 字符串类的 c_str() 方法返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17402980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:10:02  来源:igfitidea点击:

What does c_str() method from string class returns?

c++

提问by Amit Bhaira

I want to access starting address of the array that is maintained by the string class.

我想访问由字符串类维护的数组的起始地址。

string str="hey";
char* pointer=(char*)str.c_str();
  1. Is the pointer pointing to the address of the array(maintained by the string class)? or string class will create a new array from dynamic memory and copy the existing string into it and return it's address?

  2. If this is not the right way, then how to access the starting address of the array that is maintained by the string class?

  1. 指针是否指向数组的地址(由字符串类维护)?或者字符串类将从动态内存中创建一个新数组并将现有字符串复制到其中并返回它的地址?

  2. 如果这不是正确的方法,那么如何访问由字符串类维护的数组的起始地址?

回答by Andrian Nord

In C++11standard it's explicitly stated that .c_str()(as well as newer .data()) shall return pointer to the internal buffer which is used by std::string.

C++11标准中,明确规定.c_str()(以及更新的.data())应返回指向由std::string.

Any modification of the std::string after obtaining the pointer via .c_str()mayresult in said char *returned to became invalid (that is - if std::stringinternally had to reallocate the space).

在获取指针 via 后对 std::string 的任何修改都.c_str()可能导致所述char *返回无效(即 - 如果std::string内部必须重新分配空间)。

In previous C++ standards implementation is allowed to return anything. But as standard do not require user to deallocate the result, I've never seen any implementation returning anything newly allocated. At least GNU gcc's and MSVC++'s STL string are internally zero-terminated char arrays, which are returned by c_str().

在以前的 C++ 标准实现中,允许返回任何内容。但是作为标准不要求用户取消分配结果,我从未见过任何实现返回任何新分配的内容。至少 GNU gcc 和 MSVC++ 的 STL 字符串是内部以零结尾的字符数组,它们由c_str().

So it's safe to assume (with normal for C++ caution) that in any version of C++ in any it's implementation .c_str()will return internal buffer.

因此可以安全地假设(对于 C++ 来说是正常的),在任何版本的 C++ 中,任何它的实现.c_str()都将返回内部缓冲区。

In other words - you should never ever keep the value of the .c_str()unless you are 100% sure it's won't change it's size anytime in future (unless it's a const, that is).

换句话说 - 你永远不应该保留 的值,.c_str()除非你 100% 确定它在未来的任何时候都不会改变它的大小(除非它是 a const,也就是说)。

P.S. BTW, you should never ever do char* pointer=(char*)str.c_str();. It's const char *and you shall not modify the contents, partly because the above - you may end-up overwriting memory of some other object orcorrupting internal state of std::string, in case implementation doing something fancy, like indexing characters for faster .find()(newer seen that, but hey - that's an encapsulation!)

PS BTW,你永远不应该这样做char* pointer=(char*)str.c_str();。它是const char *并且您不应修改内容,部分原因是上述原因 - 您可能最终会覆盖某些其他对象的内存破坏 的内部状态std::string,以防实现做一些花哨的事情,例如为更快的索引字符.find()(较新看到的,但是嘿- 这是一个封装!)

回答by Sebastian Mach

NOTE: My answer is only correct for pre-C++11. For C++11, thisis the correct answer.

注意:我的答案仅适用于pre-C++11。对于 C++11,是正确答案。



  1. It returns a C-string with null-termination. std::stringitself is not null-terminated, so an implementation is allowed to (and probably will) return a newly allocated array of const char. If the creating std::stringgoes out of scope or if it is mutated, the c_str()-returned string is invalidated.

  2. data()returns the data, but beware it is not a null-terminated string.

  1. 它返回一个空终止的 C 字符串。std::string本身不是以空值结尾的,因此允许(并且可能会)返回一个新分配的const char. 如果创建std::string超出范围或发生变异,则c_str()返回的字符串无效。

  2. data()返回数据,但要注意它不是以空字符结尾的字符串。

In neither case, you are supposed to tweak that data, both data()and c_str()return pointers to const char, and you really shouldn't.

在这两种情况下,您都应该调整该数据data()c_str()返回指向 的指针const char,而您确实不应该这样做。

E.g., std::strings are allowed to be reference counted strings (though this is not common anymore) or may even use funky indexing schemes on their data (never seen that, though).

例如,std::string允许 s 是引用计数的字符串(尽管这不再常见),或者甚至可以对其数据使用时髦的索引方案(尽管从未见过)。

回答by Mats Petersson

If you actually want the "data" inside the string, then string::data()is the function you are looking for.

如果您确实想要字符串中的“数据”,那么string::data()就是您要查找的函数。

Note however, that like c_str(), it is a constpointer to the data - you are not supposed to modify this data.

但是请注意,就像 一样c_str(),它是const指向数据的指针 - 您不应该修改此数据。

回答by Aman Deep Gautam

You can get address of the starting of string by char *address = &str[0];. No need to convert string to c-string representation.

您可以通过 获取字符串的起始地址char *address = &str[0];。无需将字符串转换为 c 字符串表示。

回答by Jun Zhou

In C++11, the pointer returned points to the internal array currently used by the string object to store the characters that conform its value.

在 C++11 中,返回的指针指向字符串对象当前使用的内部数组,用于存储符合其值的字符。

Refer to http://www.cplusplus.com/reference/string/string/c_str/for more details.

有关更多详细信息,请参阅http://www.cplusplus.com/reference/string/string/c_str/