这是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. HTTPS站点搭建教程:Win7/Windows Server 2008R2

    本文将由笔者为各位读者介绍在win7/windows server 2008R2环境下使用SSL加密协议建立WWW站点的全过程:https SSL证书安装的搭建以及本地测试环境. 要想成功架设SSL安 ...

  2. LA-3029(扫描线)

    题意: 给定一个n*m的矩阵,一些格子是空地“F”,一些是障碍"R",找出一个全部由F组成的面积最大的子矩阵; 思路: 对每个格子维护up[i][j],le[i][j],ri[i] ...

  3. Rational Rose 2007 &Rational Rose 2003 下载及破解方法和汉化文件下载

    Rational Rose 2007 &Rational Rose 2003 下载及破解方法和汉化文件下载 分类: 其它2010-08-10 13:28 12926人阅读 评论(4) 收藏 举 ...

  4. 任务36:应用Jwtbearer Authentication

    任务36:应用Jwtbearer Authentication D:\MyDemos\jesse 新建项目:dotnet new webapi --name JwtAuthSample VS2017运 ...

  5. HDU 5879 Cure (数论)

    题意:给定n,求前 n 项 1/(k*k) 的和. 析:由于这个极限是 PI * PI / 6,所以我们可以找到分界点,然后计算就好. 代码如下: #pragma comment(linker, &q ...

  6. CodeForces 712A Memory and Crow (水题)

    题意:有一个序列,然后对每一个进行ai = bi - bi + 1 + bi + 2 - bi + 3.... 的操作,最后得到了a 序列,给定 a 序列,求原序列. 析:很容易看出来,bi = ai ...

  7. 模板 - 数学 - 快速傅里叶变换/快速数论变换(FFT/NTT)

    先看看. 通常模数常见的有998244353,1004535809,469762049,这几个的原根都是3.所求的项数还不能超过2的23次方(因为998244353的分解). 感觉没啥用. #incl ...

  8. Codeforces - 1114B - Yet Another Array Partitioning Task - 构造 - 排序

    https://codeforces.com/contest/1114/problem/B 一开始叫我做,我是不会做的,我没发现这个性质. 其实应该很好想才对,至少要选m个元素,其中m个作为最大值,从 ...

  9. java笔记1:准备工作:java历史、Java环境、java编辑器、cmd常用命令

    java的历史 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称. 由James Gosling和同事们共同研发,并在1995年正 ...

  10. (7)awk命令(每周一个linux命令系列)

    简介 awk是一个强大的文本分析工具,尤其是在应对格式化比较好的日志文件时,简单来说awk就是把文件逐行的读入,以空格为默认分隔符(也可以指定分隔符)将每行切片处理. 语法 awk [选项参数] 's ...