首页 > 其他分享 >`static_cast` caution

`static_cast` caution

时间:2024-03-17 22:33:33浏览次数:14  
标签:instance address cast static memory caution d2 class

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

标签:instance,address,cast,static,memory,caution,d2,class
From: https://www.cnblogs.com/chengxin1985/p/18079335

相关文章

  • JAVA面向对象高级:static注意事项
    packagecom.itheima.static1;publicclassStudent{staticStringschoolName;doublescore;//实例变量//1.类方法中可以直接访问类的成员,不可以直接访问实例成员publicstaticvoidprinthelloworld(){//注意:同一个类中,访问类成员,可以省略类......
  • JAVA面向对象高级:static修饰成员方法 真正搞懂main方法 类方法实例方法应用场景
         真正搞懂main方法    类方法实例方法应用场景类方法最常见的应用场景是做工具类      ......
  • 【论文阅读】Autoformer Decomposition Transformers with Auto-Correlation for Long
    原始题目:Autoformer:DecompositionTransformerswithAuto-CorrelationforLong-TermSeriesForecasting中文翻译:Autoformer:用于长期序列预测的自相关分解变压器发表时间:2021年平台:AdvancesinNeuralInformationProcessingSystems文章链接:https://proceedings.neuri......
  • static
      静态的属性是随着字节码文件的加载而在堆内存中加载,其他没有被修饰的属性是创建对象的时候在堆里加载工具类会将构造方法私有化,防止多余操作(创建工具类对象)因为可以直接用类名调对象内存相关,静态的方法和成员在字节码文件加载的时候就加载了,而其他的需要调用的时候才......
  • static静态
    工具类......
  • 4.static应用-单例设计模式
    什么是设计模式,设计模式主要学什么,单例模式解决了什么问题?设计模式就是具体问题的最有解决方案解决了什么问题?怎么写?确保一个类只有一个对象单例怎么写,饿汉式单例的特点是什么?把类的构造器私有;定义一个类变量存储类的一个对象;提供一个类方法返回对象在获取类的对象时,对......
  • C#版开源免费的Bouncy Castle密码库
    思维导航前言项目介绍BouncyCastle介绍项目源代码创建控制台应用安装BouncyCastle包BouncyCastle使用示例项目源码地址优秀项目和框架精选DotNetGuide技术社区交流群前言今天大姚给大家分享一款C#版开源、免费的BouncyCastle密码库:BouncyCastle。项目介绍B......
  • C#版开源免费的Bouncy Castle密码库
    前言今天大姚给大家分享一款C#版开源、免费的BouncyCastle密码库:BouncyCastle。项目介绍BouncyCastle是一款C#版开源、免费的BouncyCastle密码库,开发人员可以通过该项目在他们的C#应用程序中使用BouncyCastle提供的各种密码学功能,从而加强数据的安全性和保护隐私信息。......
  • Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.Enti
    如题再做查询的时候报了这个错误。原代码如下:publicvirtualasyncTask<PagedList<ApiScope>>GetApiScopesAsync(stringsearch,intpage=1,intpageSize=10){varpagedList=newPagedList<ApiScope>();varfilteredApiScopes......
  • 【论文阅读】Informer Beyond Efficient Transformer for Long Sequence Time-Series
    原始题目:Informer:BeyondEfficientTransformerforLongSequenceTime-SeriesForecasting中文翻译:Informer:超越有效变换器进行长序列时间序列预测发表时间:2021-05-18平台:ProceedingsoftheAAAIConferenceonArtificialIntelligence文章链接:https://ojs.aaai.org/i......