该内存会被正确释放吗?

发布于 2024-09-25 19:50:52 字数 161 浏览 2 评论 0原文

我有一个对指针让我们假设 std::pair< A*, B* >* 指针对。我为它分配了内存,并在使用该对后我称之为删除指针对。

它还会调用删除 A 和删除 B 并完全释放内存吗?

如果我只调用delete A和delete B但没有删除指针对那么这是内存泄漏吗?

I have a pair pointer let us suppose std::pair< A*, B* >* pointerpair. I allocated it memory and after using the pair i call delete pointerpair.

Will it also call delete A and delete B and will be freeing the memory completely ?

if i only call delete A and delete B but no delete pointerpair then is it a memory leak ?

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

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

发布评论

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

评论(2

多像笑话 2024-10-02 19:50:52

不会

。但是,在STL和Boost中,有一类叫做智能指针的类,它可以自动删除其中存储了指针的对象。然而,在这些智能指针类之外,存储在 STL / Boost 容器中的任何原始指针都被简单地视为指针。例如,您可以定义一组指向字符串的原始指针。该集合将使用指针值排序,而不是字符串的字典顺序(除非您提供比较函子)。

No.

However, in STL and Boost, there is a family of classes called smart pointers, which can automatically delete the objects whose pointers are stored in them. However, outside those smart pointer classes, any raw pointers stored in STL / Boost containers are simply treated as pointers. For example, you can define a set of raw pointers to strings. This set will be sorted using the pointer values, not the lexicographical order of the strings (unless you supply your comparison functor).

混吃等死 2024-10-02 19:50:52

不,它不会自动完成。您必须明确删除它。

No. It won't do it automatically. You have to delete it explicitly.

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