C ++新的[]插入数组访问基类指针崩溃 [英] C++ new[] into base class pointer crash on array access

查看:142
本文介绍了C ++新的[]插入数组访问基类指针崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我分配一个对象,这个code正常工作。当我尝试添加数组语法,它出现segfaults。为什么是这样?在这里我的目标是从外面的世界隐藏的C类内部采用B对象的事实。我已经发布程序 codePAD 你一起玩。

 的#include<&iostream的GT;使用命名空间std;//文件1一类
{
    上市:
    虚拟无效米(){}
    虚拟〜一个(){}
};//文件2B类:公众
{
    INT X;    上市:
    无效米(){COUT<< !b \\ N的; }
};//文件3C类:公众
{
    A * S;    上市:
    //存在问题的部分
    C(){S =新型B [10]; } // S =新型B;
    无效米(){的for(int i = 0;我小于10;我++)S [I] .M(); } // S-GT&; M();
    〜C(){删除[] S; } //删除S;
    // END存在问题的部分
};//文件4INT主要(无效)
{
    C 0;    o.m();    返回0;
}


解决方案

一个问题是,我们的前pression S [I] 使用指针算法来计算所需的对象的地址。由于取值定义为指针 A ,结果是的数组正确的一个和不正确的的数组b 秒。动态绑定通过继承规定仅适用于方法,没有别的(例如,没有虚拟数据成员,没有虚拟的sizeof )。因此调用方法当s [I] .M()这个指针被设置为会是什么 I A 对象数组中为止。但是,由于在现实中的数组的一架B S,它结束了(有时)指着某处对象的中间,你会得到一个段错误(可能是在程序试图访问该对象的虚函数表)。您可能能够通过虚拟化和重载纠正问题运算符[]()。 (我不认为它通过看它是否将实际工作,虽然)。

另一个问题是在析构函数删除,出于同样的原因。你也许能够虚拟化和重载它。 (同样,刚才那突然出现在我的脑海随机的想法。可能不起作用。)

当然,铸造(由其他人的建议)也可以工作。

When I allocate a single object, this code works fine. When I try to add array syntax, it segfaults. Why is this? My goal here is to hide from the outside world the fact that class c is using b objects internally. I have posted the program to codepad for you to play with.

#include <iostream>

using namespace std;

// file 1

class a
{
    public:
    	virtual void m() { }
    	virtual ~a() { }
};

// file 2

class b : public a
{
    int x;

    public:
    	void m() { cout << "b!\n"; }
};

// file 3

class c : public a
{
    a *s;

    public:
    	// PROBLEMATIC SECTION
    	c() { s = new b[10]; } // s = new b;
    	void m() { for(int i = 0; i < 10; i++) s[i].m(); } // s->m();
    	~c() { delete[] s; } // delete s;
    	// END PROBLEMATIC SECTION
};

// file 4

int main(void)
{
    c o;

    o.m();

    return 0;
}

解决方案

One problem is that the expression s[i] uses pointer arithmetic to compute the address of the desired object. Since s is defined as pointer to a, the result is correct for an array of as and incorrect for an array of bs. The dynamic binding provided by inheritance only works for methods, nothing else (e.g., no virtual data members, no virtual sizeof). Thus when calling the method s[i].m() the this pointer gets set to what would be the ith a object in the array. But since in actuality the array is one of bs, it ends up (sometimes) pointing to somewhere in the middle of an object and you get a segfault (probably when the program tries to access the object's vtable). You might be able to rectify the problem by virtualizing and overloading operator[](). (I Didn't think it through to see if it will actually work, though.)

Another problem is the delete in the destructor, for similar reasons. You might be able to virtualize and overload it too. (Again, just a random idea that popped into my head. Might not work.)

Of course, casting (as suggested by others) will work too.

这篇关于C ++新的[]插入数组访问基类指针崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆