这是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. poj 1469 COURSES 解题报告

    题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...

  2. Oracle:通过pl/sql developer工具导入excel数据

    1.在pl/sql developer中选择工具-->ODBC导入器 2.选择需要导入的EXCEL文件(CVS也可以):用户名.口令不用管,直接点“连接”,找到要导入的xls文件 3. 选择“导 ...

  3. 书写优雅的shell脚本(五)- shell中(())双括号运算符

    在使用shell的逻辑运算符"[]"使用时候,必须保证运算符与算数之间有空格. 四则运算也只能借助:let,expr等命令完成. 今天讲的双括号"(())"结构 ...

  4. java中io类型及成熟io框架

    就io本身而言,概念上有5中模型:blocking I/O, nonblocking I/O, I/O multiplexing(select and poll), singal driven I/O ...

  5. ubuntu openjdk 7 升级 8

    /******************************************************************************* * ubuntu openjdk 7 ...

  6. Android 获取View宽度

    /***************************************************************************** * Android 获取View宽度 * ...

  7. Vue.nextTick()的正确使用

    Vue异步执行DOM更新.只要观察导数据变化,Vue将开启一个队列,并缓冲在同一事件循环中发生的所有数据改变,如果同一个watcher被多次触发,只会一次推入到队列中.这种在缓冲时去除重复数据对于避免 ...

  8. UI:KVO、KVC

    什么是KVC 什么是 KVO ? KVC:(NSKey ValueCoding)”键-值  编码“是一种间接的访问对象属性(字符串表征)的机制.对象的属性都可以通过使用KVC机制用相同的方式访问.我们 ...

  9. 洛谷 - P1217 - 回文质数 - 枚举

    https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...

  10. mui 中在windows中打开新页面

    <!doctype html><html> <head> <meta charset="utf-8"> <title>& ...