从逆向角度看C++--C++中类与继承在汇编中的关系

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

class employee{
public:
    employee(){
        printf("employee()!\n");
    }
    ~employee(){  
        printf("~employee()\n");
    }
};

class manager : public employee{
public:
    manager(){
        printf("manager()!\n");
    }
    ~manager(){   
        printf("~manager()!\n");
    }
};

int main(int argc, char* argv[]){
    manager My;   // 创建manager对象
    getchar();    // 等待用户按键
    return 0;
}

汇编先跳到main函数里来

然后点金main里的第一个跳转

先获取manager这个类 

这里是初始化这个类

析构函数是什么

把析构函数想象成对象的 "临终遗言" 或 "清理工"

  • 构造函数 = 对象"出生"时的准备工作

  • 析构函数 = 对象"死亡"时的清理工作

析构函数:~ + 类名

指针与内存

  • 调试时关注内存内容而非寄存器:

    • 寄存器(如 EAX, ECX)变化快、临时存储。

    • 内存布局才是理解类对象、继承与 this 的核心。

  • 所有函数调用、对象访问在汇编中通过指针操作完成。

类继承在汇编中的表现

#include "stdafx.h"
#include <stdio.h>  
#include <windows.h>


struct MyStruct
{
    int x;
    int y;
};  

//函数在结构体外部(实际在外部)
void Max(MyStruct* str)
{
    if (str->x > str->y)
        printf("%d", str->x);
    else
        printf("%d", str->y);
}

int main(int argc, TCHAR* argv[])
{
    MyStruct haha;
    haha.x = 1;
    haha.y = 2;
    Max(&haha);
    printf("%d\n", sizeof(haha));  // 添加换行,输出更清晰
    return 0;
}

用od打开

进入main函数

看一下eax

eax指向的位置是1,地址再加4指向2,把eax作为参数压入到max函数

进入到max

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值