環境: - 的GNU/Linux(Ubuntu的14.04和Mageia 5) - GCC 4.9.2(Mageia下) - 系統QT5和提高爲什麼我的C++程序在發佈模式下崩潰時,結構有默認的構造函數?
截至最近我的計劃是在調試和發佈沒有問題的工作模式。無關的變化(並沒有很好地識別)使其崩潰,但僅在發佈模式下。不在調試中。
在調試中,valgrind不會發出任何錯誤信號。在發行版中,它會報告使用非初始化數據,但在方法開始時使用。通過系統的搜索,我能追查到使用下面的結構組成:
struct LIMA_LINGUISTICANALYSISSTRUCTURE_EXPORT LinguisticElement {
StringsPoolIndex inflectedForm;
StringsPoolIndex lemma;
StringsPoolIndex normalizedForm;
LinguisticCode properties;
MorphoSyntacticType type;
bool operator==(const LinguisticElement& le) const;
bool operator<(const LinguisticElement& le) const;
};
StringsPoolIndex和LinguisticCode被定義爲:
BOOST_STRONG_TYPEDEF(uint64_t, StringsPoolIndex);
BOOST_STRONG_TYPEDEF(uint32_t, LinguisticCode);
和MorphoSyntacticType是一個枚舉。
如果我添加顯式構造函數和一個運算符=,崩潰消失,valgrind停止發出錯誤信號。
LinguisticElement::LinguisticElement() :
inflectedForm(0),
lemma(0),
normalizedForm(0),
properties(0),
type(NO_MORPHOSYNTACTICTYPE)
{
}
LinguisticElement::LinguisticElement(const LinguisticElement& le) :
inflectedForm(le.inflectedForm),
lemma(le.lemma),
normalizedForm(le.normalizedForm),
properties(le.properties),
type(le.type)
{
}
LinguisticElement& LinguisticElement::operator=(const LinguisticElement& le)
{
inflectedForm = le.inflectedForm;
lemma = le.lemma;
normalizedForm = le.normalizedForm;
properties = le.properties;
type = le.type;
return *this;
}
我不明白爲什麼會發生這種情況,因爲如果我理解的很好,我的實現與編譯器生成的實現相同。或者我錯了?
發佈和調試模式在編譯器優化方面有所不同。在發佈模式中禁用優化,然後重試。 – adlag
編譯器生成的構造函數不會初始化結構成員。 –