zoukankan      html  css  js  c++  java
  • How `delete’ works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=21

    February 16, 2013

    How `delete’ works ?

    Filed under: c++ — Tags: , , , — Raison @ 2:09 am

    (original works by Peixu Zhu)

    1.  case of class without virtual destructor.

    • calling desctructors from child to parent as a chain, which destructor to call is determined at compiling time according to the type of the instance.
    • call `free‘ function to free the memory, given the address of the instance.

    2.  case of class with virtual destructor.

    • check the virtual pointer table, and call the destructor in table. when the destructor finish working, it will replace the virtual pointer table with it’s parent’s virtual pointer table, and then calling it’s parent’s desctructor like a chain.
    • after chained calling of desctructors, call `free‘ function to free the allocated memory, given the address of instance minus sizeof(void*).

    3.  case of arrayed instances without virtual destructor.

    • check address of first instance minus sizeof(void*), to get the count of cells/instances, (i.e., `n’).
    • call each instance’s destructor, from n-1 to 0.
    • call `free‘ function to free the allocated memory, given the address of the first instance minus sizeof(void*).

    4.  case of arrayed instances with virtual destructor.

    • check address of first instance minus sizeof(void*), to get the count of cells/instances, (i.e., `n’).
    • call each instance’s chained destructor, from n-1 to 0.
    • call `free‘ function to free the allocated memory, given the address of the first instance minus sizeof(void*).

    5.  calling virtual desctructors.
    after finish working code in the destructor function, if there’s parent class, and the parent class has virtual destructor, set the virtual pointer table to be the virtual pointer table of parent class, then call the parent class’s destructor recursively like a chain. This is a must, because virtual methods called in virtual parent desctrutor should be the version in parent class.

  • 相关阅读:
    那些书本上不曾告诉你的秘密
    附件十四面的数学模型与自动化算法分析
    ICAO 附件十四面课件分享
    风螺旋线的公式与特性
    How to describe the wind sprial in computer system?
    性能分析中看到螺旋线的影子
    风螺旋线的画法比较(三)
    风螺旋线的画法比较(二)
    风螺旋线的画法比较(一)
    网卡工作原理和wireshark混杂模式
  • 原文地址:https://www.cnblogs.com/raison/p/5573127.html
Copyright © 2011-2022 走看看