mercredi 8 mars 2023

Why are virtual destructors two function pointers in the virtual function table

When I debug with the following code, I found that the virtual function table has two virtual function pointers through gdb. Why and I don't know what the unknow_func_ptr is?

#include <stdio.h>

typedef void (*Func)(void);

class Base
{
public:
    Base() {}

    virtual void a() { printf("%s()\n", __func__); }
    virtual void b() { printf("%s()\n", __func__); }
    virtual void c() { printf("%s()\n", __func__); }

    virtual ~Base() { printf("%s()\n", __func__); }

    int number;
};

int main(int argc, char **argv)
{
    Base *base = new Base();

    long *vptr = reinterpret_cast<long *>(base);

    long *virtual_table_address = reinterpret_cast<long *>(*vptr);

    Func base_a_func_ptr = reinterpret_cast<Func>(*(virtual_table_address + 0));

    Func base_b_func_ptr = reinterpret_cast<Func>(*(virtual_table_address + 1));

    Func base_c_func_ptr = reinterpret_cast<Func>(*(virtual_table_address + 2));

    Func base_destructure_func_ptr_1 = reinterpret_cast<Func>(*(virtual_table_address + 3));
    
    Func base_destructure_func_ptr_2 = reinterpret_cast<Func>(*(virtual_table_address + 4));

    Func unknow_func_ptr_1 = reinterpret_cast<Func>(*(virtual_table_address + 5));

    Func unknow_func_ptr_2 = reinterpret_cast<Func>(*(virtual_table_address + 6));
    return 0;
}

gdb The following is the result of vscode debugging vscode debug

Aucun commentaire:

Enregistrer un commentaire