将 auto_ptr 与 new char[n] 一起使用是否错误
如果我使用声明一个临时自动删除字符缓冲区,
std::auto_ptr<char> buffer(new char[n]);
那么当缓冲区超出范围时,该缓冲区将自动删除。 我假设使用delete删除了缓冲区。
然而缓冲区是使用 new[] 创建的,因此严格来说应该使用 delete[] 删除缓冲区。
这种不匹配有什么可能导致内存泄漏?
If I declare a temporary auto deleted character buffer using
std::auto_ptr<char> buffer(new char[n]);
then the buffer is automatically deleted when the buffer goes out of scope. I would assume that the buffer is deleted using delete.
However the buffer was created using new[], and so strictly speaking the buffer should be deleted using delete[].
What possibility is there that this mismatch might cause a memory leak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在用 new[] 分配的指针上调用删除的行为是 未定义。 正如您所假设的,当智能指针超出范围时,auto_ptr 确实会调用delete。 您需要担心的不仅仅是内存泄漏——崩溃和其他奇怪的行为也是可能的。
如果不需要转移指针的所有权,Boost的scoped_array 类可能就是您正在寻找的。
The behaviour of calling delete on a pointer allocated with new[] is undefined. As you assumed, auto_ptr does call delete when the smart pointer goes out of scope. It's not just memory leaks you have to worry about -- crashes and other odd behaviours are possible.
If you don't need to transfer the ownership of the pointer, Boost's scoped_array class might be what you're looking for.
我会使用 char 向量作为缓冲区。
基本上,如果不需要,您甚至不想调用 new。
向量提供了一个运行时大小的缓冲区,您可以像数组(缓冲区)一样使用它。
最好的部分是向量会在自身之后进行清理,并且标准保证向量中的所有元素都将位于连续存储中。 完美的缓冲区。
或者更正式地说,保证是:
I would use a vector of char as the buffer.
Basically you don't even want to call new if you don't need to.
A vector provides a run-time sized buffer that you can use just like an array (buffer).
The best part is that the vector cleans up after itself and the standard guarantees that all element in the vector will be in contigious storage. Perfect for a buffer.
Or more formaly the guarantee is:
这会产生未定义的行为(可能比内存泄漏更糟糕,例如堆损坏)尝试 boost 的scoped_array 或shared_array 代替。
That yields undefined behaviour (could be worse than memory leak, for instance heap corruption) try boost's scoped_array or shared_array instead.
对使用 new[] 分配的数据调用删除是未定义的。 这意味着编译器可以生成可以执行任何操作的代码。 然而,在这种情况下,它可能会起作用,因为不需要破坏数组中的各个字符,只需破坏数组本身。
尽管如此,由于此行为未定义,我强烈建议使用
std::vector
或boost::scoped_array; / boost::shared_array
代替。 在这种情况下,所有这些都是完全可行且优于使用 std::auto_ptr 的选项。 如果您使用 std::vector ,您还可以根据需要动态增长缓冲区。Calling delete on data allocated with new[] is undefined. This means that the compiler may generate code that may do anything. However in this case it probably works since there's no need to destruct the individual chars in the array, just the array itself.
Still since this behavior is undefined, I would strongly recommend using
std::vector<char>
orboost::scoped_array<char> / boost::shared_array<char>
instead. All are perfectly viable and superior options to usingstd::auto_ptr<>
in this case. If you usestd::vector
you also have the possibility of dynamically grow the buffer if required.有充分的理由不使用 std::string 吗? std::vector,正如其他人所建议的那样? 你所做的事情是错误的,但如果不知道你想要做什么,推荐其他东西是很困难的。
Is there a good reason not to use std::string? std::vector, as other have suggested? What you're doing is wrong, but without knowing what you're trying to do recommending something else is difficult.
是的,这是错误的。 用一个简单的包装纸包裹起来。
Yes, it is wrong. Wrap with a trivial wrapper.
距离提出这个问题已经过去了几年。
但我通过搜索找到了这个页面,所以我想我不妨注意一下:
std::unique_ptr 是 auto_ptr 的 C++11 替代品,可以处理使用 new[] 创建的对象的删除。
cppreference unique_ptr< /a>
A few years have passed since the question was asked.
But I hit this page from a search, so I figured I might as well note:
std::unique_ptr, the C++11 replacement for auto_ptr, can handle deletion of objects created with new[].
cppreference unique_ptr
对于一个非常简单的解决方案来说,这似乎非常复杂。 有什么问题吗
你在这里使用然后删除它 ?
或者,如果您需要更动态的解决方案,
奥卡姆剃刀,伙计。 :-)
This seems awful complex for a very simple solution. What's wrong with you using
here, and then deleting it?
Or, if you need a bit more dynamic solution,
Occam's Razor, man. :-)