“破坏” “析构函数” “解除分配”在std :: allocator? [英] Difference between "destroy" "destructor" "deallocate" in std::allocator?

查看:159
本文介绍了“破坏” “析构函数” “解除分配”在std :: allocator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ std :: allocator 中,有三种与一个常见概念相关的方法:




  • deallocate

  • 销毁

  • 破坏器



我想知道:

    $
  1. b $ b

  2. >

    谢谢!






    编辑:更具体的疑问:



    对不起,先归纳一下,以下是一些我不明白的观点。


    1. 析构函数做什么?文档没有讨论调用析构函数时是否自动释放内存

    2. destroy 用于调用对象,这里的对象是什么意思?

    再次感谢!


    < div只是来自 cppreference.com文档为我解释这些差异非常清楚




    1.析构函数是什么?关于当调用析构函数时是否会自动释放内存


    <$ c $


    code> destroy 用于调用对象上的析构函数,这里的对象是什么意思?


    再次引用详细文档

      void destroy(pointer p); // 1)
    template<类U> // 2)
    void destroy(U * p);




    调用p指向的对象的析构函数

    1)调用((T *)p) - >〜T()

    2)调用 p - >〜U()


    std :: allocator 实例管理的 T 类型的对象。


    In C++ std::allocator, there are three methods relating to a common concept:

    • deallocate
    • destroy
    • destructor

    I want to know:

    1. How are they different from each other from the memory management perspective?
    2. when should I use this but not that?

    Thank you!


    Edit: More specific doubts:

    I am sorry to generalize it at first, here are some points I don't understand.

    1. What does destructor do? The documentation didn't talk about whether the memory will be released automatically when the destructor is called
    2. The destroy is used to call the destructor on a object, what does the "object" here means?

    Thank you again!

    解决方案

    Just the brief descriptions from the cppreference.com documentation explain the differences very clearly for me

    "1. What does destructor do? The documentation didn't talk about whether the memory will be released automatically when the destructor is called"

    Any memory occupied by the std::allocator instance will be released as usual.

    "2. The destroy is used to call the destructor on a object, what does the "object" here means?"

    Again to cite the detailed documentation

     void destroy( pointer p ); // 1)
     template< class U >        // 2)
     void destroy( U* p );
    

    Calls the destructor of the object pointed to by p
    1) Calls ((T*)p)->~T()
    2) Calls p->~U()

    Object in this context means an object of type T managed by the std::allocator instance.

    这篇关于“破坏” “析构函数” “解除分配”在std :: allocator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆