程序员的故乡

 

`static_cast` caution

static_cast caution

It is likely to lead unexpected behavior and maybe dangerous to invoke static_cast on wrong C++ object. Below example demostrates it.

On the second invocation of foo, foo(d2), the instance of class D2 is casted into instance of class D1 and the memory address for access to member variable b of D1 is calculated as d2's address + offset of b. The resulted address is actually out of the available memory of instance d2 because d2 is instance of class D2 which is smaller than class D1(D1 has a big member variable arr). That causeses unexpected behavior: if the memory which the address points is already allocated to the current process, the wrong data(the memory is occupied by another objects or values) are read; otherwise a "memory access violation" exception occurs.

#include <iostream>
#include <array>

using namespace std;

class Base
{
public:
    virtual void say()
    {
        cout << "hello, base;" << endl;
    }
};

class D1 : public Base
{
public:
    char a = '*';
    std::array<char, 10000> arr{};
    char b = '+';

public:
    void say() override
    {
        cout << "hello, D1;" << a << endl;
    }

    void jump()
    {
        cout << "jump " << b << endl;
    }
};

class D2 : public Base
{
public:
    long long v = 7777;

public:
    void say() override
    {
        cout << "hello, D2;" << v << endl;
    }
};

void foo(Base & bb);

int main()
{
    D1 d1;
    D2 d2;
    foo(d1);
    foo(d2); // cause memory access violation!!!
}

void foo(Base& bb)
{
    D1& d = static_cast<D1&>(bb);
    d.jump();
}

In my experiment, the "memory access violation" exception happens.
image

posted on   程鑫  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本

导航

统计

点击右上角即可分享
微信分享提示