【发布时间】:2015-11-02 07:15:18
【问题描述】:
我使用的是 GCC 3.4.3。该设备是基于ARM9/ARM11的POS终端PAX S80。下一个测试代码编译正常,但在运行时调用覆盖函数时出现异常。
class TBase {
public:
int a;
virtual void Foo() {a = 1;}
void Exec() {Foo();}
};
class TDerived : public TBase {
public:
virtual void Foo() {a = 2;}
};
TBase *Base; //pointer to object in heap
TBase Base2; //static object
TDerived *Derived; //pointer to object in heap
TDerived Derived2; //static object
int main() {
Base = new TBase;
Base->Exec(); //this passes okay
Base2.Exec(); //this passes okay
Derived = new TDerived;
Derived->Exec(); //this passes okay
Derived2.Exec(); //here I get an exception and the app crashes
return 0;
}
这意味着我不能使用静态对象(Derived2)。是的,我可以在代码中创建对象(Derived),但它会使代码复杂化,因为我需要使用“new”运算符来实例化对象。
有什么技巧可以避免这个问题吗?
顺便说一句,我在 ARM926 的 Keil 编译器上没有这个问题。不幸的是,我无法为此设备选择编译器,只能选择 GCC 3.4.3。
感谢您的任何想法!
【问题讨论】:
-
你得到了什么异常?
-
顺便说一句,你确定
//static object in application stack吗?我不声称对“PAX S80”一无所知,但通常全局变量在堆上的分配方式与通过new相同。 -
是的,你是对的。我已经更新了我的问题。
-
异常的信息是这样的:“WARNING! APP EXCEPTION DFSR:000000fd IFSR:00000000 2821e954 80000010, 80000197”
-
这在 i386-64 上工作得非常好,而且这个功能非常基本,在任何拱门上都不应该有区别。我怀疑它可能是一个编译器错误 - 你应该搜索 GCC 的错误数据库,看看你是否找到类似的东西。
标签: c++ gcc heap-memory virtual