[英]Strange issue using RWTValHashMap with g++ on Linux
我正在使用g ++ 4.3和Rogue Wave庫在Linux系統上編寫一個簡單的測試程序。 我在這里面臨的問題是可以編譯以下代碼,但是在運行它時,它將在此行上彈出分段錯誤:_aClasses.insertKeyAndValue(100,1000);
當我使用aCC編譯器在HPUX計算機上運行相同的片段代碼時。 它運行平穩,這讓我感到困惑。 是因為g ++初始化靜態變量的方式與aCC不同嗎? 有人知道這是怎么回事嗎? 提前致謝。
xx
#include <rw/tvhdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
using namespace std;
class A
{
public :
A();
static void add();
struct long_hash {
unsigned long operator() (const long& x) const { return x;};
};
struct long_equal {
RWBoolean operator() (const long& x, const long& y) const { return x==y;};
};
private:
static RWTValHashMap<long, long, long_hash, long_equal> _aClasses;
};
A.cxx
#include "A.hxx"
RWTValHashMap<long, long, A::long_hash, A::long_equal> A::_aClasses;
A::A()
{
cout<<"init A"<<endl;
}
void A::add()
{
_aClasses.insertKeyAndValue(100,1000);
}
xx
class B
{
public:
B();
};
xx
#include "B.hxx"
#include "A.hxx"
B::B()
{
A::add();
}
Main.cxx
#include "A.hxx"
#include "B.hxx"
static B c;
int main() {
cout<<"main"<<endl;
return 0;
}
沒有指定來自不同翻譯單元(本質上是不同的cpp / cxx文件)的靜態成員的初始化順序。 因此, static B c
還是RWTValHashMap<long, long, A::long_hash, A::long_equal> A::_aClasses
對於不同的編譯器而言,將首先被初始化,並且在使用同一編譯器時可能會有所不同。 幸運的是,您以前的編譯器總是按所需的順序初始化它們。
避免這種情況的一種方法是使用“首次使用成語時構造”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.