c++ char 数组是否超出范围?

发布于 2024-07-20 16:44:43 字数 203 浏览 3 评论 0原文

我有一个方法,需要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林散布 2024-07-27 16:44:43

不要删除从 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)).

我的鱼塘能养鲲 2024-07-27 16:44:43

string.c_str() 返回的 char 数组以 null 结尾。 如果tinyXML 的函数采用非 null 结尾的 char* 缓冲区,那么您可能会得到一些意外的行为。

const char* c_str () const;

获取等效的 C 字符串

生成一个空终止序列
具有相同的字符(c字符串)
内容作为字符串对象和
将其作为指向数组的指针返回
字符。

终止空字符是
自动附加。

不,它不需要被释放。 String 的析构函数会为你做到这一点。

返回的数组指向一个
具有所需的内部位置
该序列的存储空间
字符加上其终止符
空字符,但此中的值
数组不应被修改
计划并仅被授予留下来
直到下一次调用 a 之前保持不变
的非常量成员函数
字符串对象。

来源

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.

const char* c_str ( ) const;

Get C string equivalent

Generates a null-terminated sequence
of characters (c-string) with the same
content as the string object and
returns it as a pointer to an array of
characters.

A terminating null character is
automatically appended.

No, it does not need to be released. String's destructor does that for you.

The returned array points to an
internal location with the required
storage space for this sequence of
characters plus its terminating
null-character, but the values in this
array should not be modified in the
program and are only granted to remain
unchanged until the next call to a
non-constant member function of the
string object.

Source

¢好甜 2024-07-27 16:44:43

另一方面,
如果您不需要将 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 than str.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 by string. In this case, calling .c_str() would force string 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.

情绪失控 2024-07-27 16:44:43

std::string.c_str() 返回一个指向以 null 结尾的字符串的指针。 实际的字符数组仍然由 std::string 对象拥有,并且只要满足以下条件,它就有效:

  1. std::string 对象有效,并且
  2. 没有调用在 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 the std::string object, and it is valid as long as:

  1. The std::string object is valid, and
  2. No calls to non-const member functions on the std::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文