美文网首页
C/C++ 函数地址

C/C++ 函数地址

作者: 蓝笔头 | 来源:发表于2021-07-05 08:43 被阅读0次

C 函数

C 语言中没有类的概念,只有普通的函数。

#include <stdio.h>

void hello() {
    printf("hello world\n");
}

int main() {
    hello();
    printf(" hello = %p\n", hello);
    printf("&hello = %p\n", &hello);
}

控制台输出:

hello world
 hello = 0x559e5319f6f0
&hello = 0x559e5319f6f0

C++ 函数

C++ 函数有如下几种:

  • 1)普通函数:属于全局函数,不受具体类和对象的限制,可以直接调用。例如上面的 hello() 函数。
  • 2)普通成员函数:C++ 普通成员函数本质上是一个包含指向具体对象 this 指针的普通函数。
    -3)静态成员函数:成员函数的声明前加上 static 关键字,就变为静态成员函数。静态成员函数不包含指向具体对象的 this 指针。
  • 4)虚函数:成员函数的声明前加上 virtual 关键字,就变为虚函数。

提示:虚函数主要是用来实现多态的。

普通成员函数

#include <iostream>
using namespace std;

class Base {
public:
    void a() { cout << "Base a()" << endl; }
};

int main()
{
    Base base1;
    base1.a();
    printf("&base1 = %p\n", &base1);
    printf("&Base::a = %p\n", &Base::a);
    return 0;
}

控制台输出:

Base a()
&base1 = 0x7ffda2c48a4f
&Base::a = 0x564baa84ba62

静态成员函数

#include <iostream>
using namespace std;

class Base {
public:
    static void a() { cout << "Base a()" << endl; }
};

int main()
{
    Base base;
    Base::a();
    printf("&base = %p\n", &base);
    printf(" Base::a = %p\n", Base::a);
    printf("&Base::a = %p\n", &Base::a);
    return 0;
}

控制台输出:

Base a()
&base = 0x7ffcb939558f
 Base::a = 0x555977ec2a4a
&Base::a = 0x555977ec2a4a

虚函数

#include <iostream>
using namespace std;

class Base {
public:
    virtual void a() { cout << "Base a()" << endl; }
    virtual void b() { cout << "Base b()" << endl; }
};

class Derive : public Base {
public:
    virtual void a() { cout << "Derive a()" << endl; } // 覆盖Base::a()
    virtual void c() { cout << "Derive a()" << endl; } // 覆盖Base::a()
};

1)直接取地址。

int main()
{
    cout << "-----------打印 class Base------------" << endl;
    Base base;
    printf("&base = %p\n", &base);
    printf("&Base::a = %p\n", &Base::a);
    printf("&Base::b = %p\n", &Base::b);

    cout << "-----------打印 class Derive------------" << endl;
    Derive derive;
    printf("&derive = %p\n", &derive);
    printf("&Derive::a = %p\n", &Derive::a);
    printf("&Derive::b = %p\n", &Derive::b);
    printf("&Derive::c = %p\n", &Derive::c);
    return 0;
}

控制台输出:

-----------打印 class Base------------
&base = 0x7ffeb36c8498
&Base::a = 0x1
&Base::b = 0x9
-----------打印 class Derive------------
&derive = 0x7ffeb36c8490
&Derive::a = 0x1
&Derive::b = 0x9
&Derive::c = 0x11

对于 virtual function(虚函数), 其地址在编译时期是未知的,所以对于 virtual member function(虚成员函数)取其地址,所能获得的只是一个索引值。

2)通过虚函数表取地址:

int main()
{
    typedef void(*Fun)(void);
    Derive d1;
    Fun** vt_ptr = (Fun**)&d1;

    cout << "-----------第一次打印虚函数地址------------" << endl;
    printf("&d1 : %p\n", &d1);
    for (int i = 0; i < 3; i++) {
        printf("[d1] vptr[%d] : %p\n", i, *(*vt_ptr + i));
    }

    Derive d2;
    vt_ptr = (Fun**)&d2;
    cout << "-----------第二次打印虚函数地址------------" << endl;
    printf("&d2 : %p\n", &d2);
    for (int i = 0; i < 3; i++) {
        printf("[d2] vptr[%d] : %p\n", i, *(*vt_ptr + i));
    }

    return 0;
}

控制台输出:

-----------第一次打印虚函数地址------------
&d1 : 0x7ffddd4fc1f8
[d1] vptr[0] : 0x5583e3383ccc
[d1] vptr[1] : 0x5583e3383c94
[d1] vptr[2] : 0x5583e3383d04
-----------第二次打印虚函数地址------------
&d2 : 0x7ffddd4fc1f0
[d2] vptr[0] : 0x5583e3383ccc
[d2] vptr[1] : 0x5583e3383c94
[d2] vptr[2] : 0x5583e3383d04

参考

相关文章

  • C/C++ 函数地址

    C 函数 C 语言中没有类的概念,只有普通的函数。 控制台输出: C++ 函数 C++ 函数有如下几种: 1)普通...

  • C++ 函数

    原文地址:C++ 函数 函数是一组一起执行一个任务的语句。每个 C++ 程序都至少有一个函数,即主函数 main(...

  • windows逆向3

    VC 程序内存和编译的一些特征C++ 构造函数C++ 成员函数C++ 析构函数C++ 全局对象的构造C++ 全局对...

  • 超轻量操作系统OneOS-Lite对C++的支持

    简介 c++组件是为了支持编译c++源文件。该组件把全局对象的构造函数放到了指定的段地址中,以支撑c++的正常使用...

  • C++ 引用(2)

    引用是c++对c的重要扩充。在c/c++中指针的作用基本都是一样的,但是c++增加了另外一种给函数传递地址的途径,...

  • c++ 指针

    原文地址摘要:这篇文章详细介绍C/C++的函数指针,请先看以下几个主题:使用函数指针定义新的类型、使用函数指针作为...

  • C/C++ 取整函数ceil(),floor()2019-10-

    转自: C/C++ 取整函数ceil(),floor() C/C++ 取整函数ceil(),floor() #in...

  • 一些函数

    cmp函数 C++ sort cmp函数 - lzz的博客 - CSDN博客 浅谈C/C++排序函数中cmp()比...

  • C#委托

    在C和C++中,只能提取函数的地址,并作为一个参数传递它。C没有类型安全性。可以把任何函数传递给需要函数指针的方法...

  • CTF Pwn中的 UAF 及 pwnable.kr UAF w

    考察点 虚函数的内存地址空间 UAF 前置知识1:虚函数的内存地址空间 在C++中,如果类中有虚函数,那么它就会有...

网友评论

      本文标题:C/C++ 函数地址

      本文链接:https://www.haomeiwen.com/subject/atyvultx.html