c++ char 数组是否超出范围?
我有一个方法,需要一个 const char 指针作为输入(不是空终止)。 这是我在项目中使用的库(TinyXML)的要求。 我从 string.c_str()
方法调用中获取此方法的输入。
这个char
指针需要删除吗? 调用完成后,字符串立即超出范围; 所以字符串应该用它的析构函数调用来删除它,对吗?
I have a method that requires a const char
pointer as input (not null terminated). This is a requirement of a library (TinyXML) I'm using in my project. I get the input for this method from a string.c_str()
method call.
Does this char
pointer need to be deleted? The string goes out of scope immediately after the call completes; so the string should delete it with its destructor call, correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要删除从 std::string::c_str 获得的内存。 该字符串负责(它完全有可能为您提供了一个指向其内部缓冲区的指针,因此如果您删除了它,那将是一件坏事(tm))。
Do not delete the memory you get from std::string::c_str. The string is responsible for that (and it is entirely possible it gave you a pointer to its internal buffer, so if you deleted it, that would be a bad thing (tm)).
string.c_str()
返回的 char 数组以 null 结尾。如果tinyXML 的函数采用非 null 结尾的 char* 缓冲区,那么您可能会得到一些意外的行为。不,它不需要被释放。 String 的析构函数会为你做到这一点。
来源
The char array returned by
string.c_str()
is null terminated.If tinyXML's function takes a not null terminated char* buffer, then your probably gonna get some unexpected behaviour.No, it does not need to be released. String's destructor does that for you.
Source
另一方面,
如果您不需要将 char 指针以 null 终止,那么您最好使用
str.data()
而不是str.c_str()
。 不同之处在于.data()
并不保证您得到的内容将以 null 终止。 如果您的字符串恰好占据了string
分配的内部缓冲区的整个长度,那么这非常有用。 在这种情况下,调用.c_str()
将强制string
将日期重新分配到新的更大缓冲区,该缓冲区包含足够的空间来添加'\0 ' 最后。
无论如何,您当然不应该删除返回的指针。
string
会处理这个问题。On another note,
If you don't need to char pointer to be null terminated then you're better off to used
str.data()
rather thanstr.c_str()
. The difference is that.data()
doesn't grantee that what you get is going to be null terminated. This is useful if the your string just happens to occupy the entire length of the internal buffer allocated bystring
. In this case, calling.c_str()
would forcestring
to reallocate the date to a new bigger buffer, ones that contains enough space to add the'\0'
in the end.In any rate, ofcourse you shouldn't delete the pointer returned.
string
will take care of that.std::string.c_str()
返回一个指向以 null 结尾的字符串的指针。 实际的字符数组仍然由std::string
对象拥有,并且只要满足以下条件,它就有效:std::string
对象有效,并且std::string
对象上创建非常量成员函数(即修改字符串会使之前指向的任何 C 样式字符串无效)。由字符串对象本身来分配和释放它返回给您的以 null 结尾的字符数组。
您始终可以将空终止字符串用作非空终止字符串。 毕竟,NTS 只是末尾多了一个零的非 NTS。 只要字符串按照函数的预期正确终止,它就永远不会看到“额外”的 null。
std::string.c_str()
returns a pointer to a null terminated string. The actual array of characters is still owned by thestd::string
object, and it is valid as long as:std::string
object is valid, andstd::string
object are made (i.e. modifying the string invalidates any previous C-style string pointed to).It's up to the string object itself to allocate and release the null terminated array-of-char it returns to you.
You can always use a null-terminated string as a non-null-terminated string. After all, an NTS is just a non-NTS with an extra zero at the end. As long as the string is correctly terminated as the function expects, it'll never see the "extra" null.