为什么 wstring::c_str 如果没有正确删除不会导致内存泄漏

发布于 2024-09-13 08:52:03 字数 372 浏览 2 评论 0原文

代码段 1:

wchar_t *aString() 
{
     wchar_t *str = new wchar[5];
     wcscpy(str, "asdf\0");
     return str;
}
wchar_t *value1 = aString();

代码段 2

wstring wstr = L"a value";
wchar_t *value = wstr.c_str();

如果代码段 2 中的值未被删除,则不会发生内存泄漏。但是,如果代码段 1 中的 value1 未被删除,则会出现内存泄漏。 wstring::c_str 的内部代码对我来说看起来是一样的。

Code Segment 1:

wchar_t *aString() 
{
     wchar_t *str = new wchar[5];
     wcscpy(str, "asdf\0");
     return str;
}
wchar_t *value1 = aString();

Code Segment 2

wstring wstr = L"a value";
wchar_t *value = wstr.c_str();

If value from code segment 2 is not deleted then an memory leak does not occur. However, if value1 from code segment 1 is not deleted there is a memory leak. The internal code to wstring::c_str looks the same to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

一影成城 2024-09-20 08:52:03

一条重要规则:您必须对 new 创建的任何内容使用 delete,并且不能删除其他任何内容。

wstr.c_str() 返回一个指向由 wstring 对象管理的缓冲区的指针。当字符串被销毁时,它将被释放,之后指针将不再有效。对此使用 delete 是错误的。如果修改字符串,指针也会失效。

aString() 返回一个指向使用 new[] 创建的缓冲区的指针,因此您必须在使用完它后将其删除(使用 delete[ ],以匹配 new[])。这很容易出错,这就是为什么最好使用资源管理类(例如 stringwstring、容器和智能指针)而不是传递原始指针并希望他们得到正确对待。

An important rule: you must use delete on anything that was created by new, and you mustn't delete anything else.

wstr.c_str() returns a pointer to a buffer that's managed by the wstring object. It will be deallocated when the string is destroyed, after which the pointer will no longer be valid. Using delete on this is wrong. The pointer will also be invalidated if you modify the string.

aString() returns a pointer to a buffer that was created using new[], so you must delete it when you've finished with it (using delete[], to match new[]). This is error-prone, which is why it is better practice to use resource-managing classes (like string, wstring, containers and smart pointers) rather than passing around raw pointers and hoping they are treated correctly.

从此见与不见 2024-09-20 08:52:03

因为 c_str() 返回一个指向 wstring 内部表示的指针。该类对其包含的数据保持控制。

Because c_str() returns you a pointer to the internal representation of the wstring. The class keeps control of the data it contains.

兰花执着 2024-09-20 08:52:03

取自 basic_string::c_str() 来自 MSDN 的文档

返回的 C 风格字符串不应
被修改,因为这可能会使
指向字符串的指针,或删除的字符串,
因为字符串的生命周期有限
并且由类字符串拥有

Taken from the basic_string::c_str() documentation from MSDN:

The returned C-style string should not
be
modified, as this could invalidate
the pointer to the string, or deleted,
as the string has a limited lifetime
and is owned by the class string
.

暖风昔人 2024-09-20 08:52:03

我要冒险说 wstring 不是 wchar_t,而是一个具有返回 wchar_t * 的运算符的类,因此在 wstring 的析构函数中,它可能会释放自己的 wchar_t 副本* 它返回。

I'm going to go out on a limb and say that a wstring is not a wchar_t, but instead a class that has an operator to return a wchar_t *, so in the destructor of wstring, it likely frees its own copy of the wchar_t * it returns.

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