这是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. UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

    d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...

  2. 666 专题五 AC自动机

    Problem A.Keywords Search d.n个关键字,1段描述,求描述中出现了多少关键字 s. c. /* ac自动机模板 n个关键字,1段描述,求描述中出现了多少关键字 */ #inc ...

  3. python中PIL.Image和OpenCV图像格式相互转换

    PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy image = Image.open("plane.j ...

  4. BZOJ_4609_[Wf2016]Branch Assignment_决策单调性+带权二分

    BZOJ_4609_[Wf2016]Branch Assignment_决策单调性+带权二分 Description 要完成一个由s个子项目组成的项目,给b(b>=s)个部门分配,从而把b个部门 ...

  5. 【POJ 1655】 Balancing Act

    [题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...

  6. border-collapse

    表格边框,对于别的元素加上的边框不起作用 border-collapse : separate(默认) | collapse | inherit separate : 边框独立 效果图: collap ...

  7. 链表中获取倒数第K个结点

    /* * 链表中查找倒数第K个结点.cpp * * Created on: 2018年5月1日 * Author: soyo */ #include<iostream> using nam ...

  8. (转)Eclipse4.2 Tomcat启动报错 A child container failed during start

     Eclipse4.2 Tomcat启动报错 A child container failed during start 2013-5-21 15:02:24 org.apache.catalina. ...

  9. UVaLive 6585 && Gym 100299F Draughts (暴力+回溯)

    题意:给定一个 10*10的矩阵,每一个W可以跳过一个B向对角走到#并把B吃掉,并且可以一直跳直到不能动为止,现在是W走的时候,问你最多吃几个B. 析:直接暴力+回溯,深搜就好. 代码如下: #pra ...

  10. BestCoder Round #73 (div.2)1002/hdoj5631

    题意: 给出一张 nnn 个点 n+1n+1n+1 条边的无向图,你可以选择一些边(至少一条)删除. 分析: 一张n个点图,至少n-1条边才能保证联通 所以可以知道每次可以删去1条边或者两条边 一开始 ...