简体   繁体   English

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

[英]What does c_str() method from string class returns?

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? 如果这不是正确的方法,那么如何访问由字符串类维护的数组的起始地址?

In C++11 standard 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() may result in said char * returned to became invalid (that is - if std::string internally had to reallocate the space). 通过.c_str()获取指针后对std :: string的任何修改都可能导致返回的char *变为无效(即 - 如果std::string内部必须重新分配空间)。

In previous C++ standards implementation is allowed to return anything. 在以前的C ++标准中,允许实现返回任何内容。 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() . 至少GNU gcc和MSVC ++的STL字符串是内部以零结尾的char数组,由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%确定它将来不会改变它的大小(除非它是一个const ,即)。

PS BTW, you should never ever do char* pointer=(char*)str.c_str(); PS BTW,你永远不应该做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 or corrupting 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!) 它是const char * ,你不应该修改内容,部分是因为上面 - 你可能最终覆盖某些其他对象的内存破坏std::string内部状态,以防实现做一些奇特的事情,比如索引字符更快.find() (更新看到了,但是嘿 - 这是封装!)

NOTE : My answer is only correct for pre-C++11 . 注意 :我的答案仅适用于C ++之前的11 For C++11, this is the correct answer. 对于C ++ 11, 是正确的答案。


  1. It returns a C-string with null-termination. 它返回一个带空终止的C字符串。 std::string itself is not null-terminated, so an implementation is allowed to (and probably will) return a newly allocated array of const char . std::string本身不是以null结尾的,因此允许(并且可能会)返回一个新分配的const char数组。 If the creating std::string goes out of scope or if it is mutated, the c_str() -returned string is invalidated. 如果创建std::string超出范围或者它被突变,则c_str()返回的字符串将失效。

  2. data() returns the data, but beware it is not a null-terminated string. data()返回数据,但要注意它不是以null结尾的字符串。

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指针,你真的不应该这样做。

Eg, std::string s 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被允许作为引用计数字符串(虽然这不再常见)或甚至可能对其数据使用时髦的索引方案(尽管如此)。

You can get address of the starting of string by char *address = &str[0]; 您可以通过char *address = &str[0];获取字符串开头的char *address = &str[0]; . No need to convert string to c-string representation. 无需将字符串转换为c-string表示。

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 const pointer to the data - you are not supposed to modify this data. 但请注意,与c_str()类似,它是指向数据的const指针 - 您不应该修改此数据。

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM