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

February 16, 2013

How `delete’ works ?

Filed under: c++ — Tags: c++ delete, c++ destructor, c++ internals, c++ virtual — 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.

How `delete’ works ?的更多相关文章

  1. MTK Android 计算器Calculator输入暗码!77!+,启动工厂测试apk

    Android8.0 计算器Calculator输入暗码!77!+,启动工厂测试apk 路径: packages/apps/ExactCalculator/src/com/android/calcul ...

  2. 如果调用ASP.NET Web API不能发送PUT/DELETE请求怎么办?

    理想的RESTful Web API采用面向资源的架构,并使用请求的HTTP方法表示针对目标资源的操作类型.但是理想和现实是有距离的,虽然HTTP协议提供了一系列原生的HTTP方法,但是在具体的网络环 ...

  3. MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

    前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...

  4. [Hive - LanguageManual] DML: Load, Insert, Update, Delete

    LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...

  5. How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/

    In this tutorial, I would like to help out Magento developers and clients with how to programmatical ...

  6. elasticsearch6.7 05. Document APIs(5)Delete By Query API

    4.Delete By Query API _delete_by_query API可以删除某个匹配条件的文档: POST twitter/_delete_by_query { "query ...

  7. How Autofs Works

    How Autofs Works Autofs is a client-side service that automatically mounts the appropriate file syst ...

  8. using the library to generate a dynamic SELECT or DELETE statement mysqlbaits xml配置文件 与 sql构造器 对比

    https://github.com/mybatis/mybatis-dynamic-sql MyBatis Dynamic SQL     What Is This? This library is ...

  9. after modifying system headers, please delete the module cache at

    5down votefavorite 2 I don't know how I modified a iOS SDK file, but Xcode say I did. Here is what t ...

随机推荐

  1. php连接数据库步骤

    第一步:连接数据库 $link=@mysql_connect('localhost','root','root') or die('数据库连接失败!'); echo '连接成功!'; 这里数据库连接函 ...

  2. 15个你不可不知的cmd命令

    cmd和dos: dos是Disk Operating System 磁盘操作系统的缩写,它是个人计算机上的一类操作系统.跟Windows图像界面操作系统不同的是,dos是一种面向磁盘的操作系统,并且 ...

  3. [Selenium] Explicit wait 方法

    (1)  new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator)); (2)  ne ...

  4. BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流

    BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流 Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1 ...

  5. Linux终端那件事儿

    我们将会讨论如何更好的控制用户终端:也就说是键盘输入与屏幕输出.除了这些,我们还会了解我们编写的程序如何由用户处读取输入,即使是在输入重定向的情况下,以及确保输出到屏幕的正确位置.这里所提供的一些底层 ...

  6. 【旧文章搬运】Windows内核常见数据结构(基本类型)

    原文发表于百度空间,2008-7-23 ========================================================================== 学内核从基 ...

  7. k8s-存储卷1-十二

    因为pod是有生命周期的,pod一重启,里面的数据就没有了.所以我们需要数据持久化存储. 在k8s中,存储卷不属于容器,而是属于pod.也就是说同一个pod中的容器可以共享一个存储卷. 存储卷可以是宿 ...

  8. 016--python文件处理

    一.操作文件流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 示例代码: f = open('chenli.txt') #打开文件 first_line = ...

  9. 洛谷 - P1162 - 填涂颜色 - 简单搜索

    https://www.luogu.org/problemnew/show/P1162 在外面加一圈0把0连起来,然后把所有0换成2,再从(0,0)把连通的2全部改回来. 这也是一个判断内外圈的好办法 ...

  10. hdoj1166【线段树】

    单点更新+区间求和 不多说,直接上渣code--- #include<cstdio> #include<iostream> #include<string.h> # ...