gpt4 book ai didi

c++ - C++ 头文件中空引用驱动的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:27 26 4
gpt4 key购买 nike

任何人都可以找出以下代码部分有什么问题吗:

private:
TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }
TUcdFileReader(const TUcdFileReader& r) { Fail; }

这属于我在代码中使用的 c 库 (SNAP) 中的头文件。当我在基于 Linux 的系统中编译代码时,出现以下警告:

Snap-2.1/glib-core/unicode.h(1678): warning #327: NULL reference is not allowed
TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }

这总体上导致我出现如下段错误:(我怀疑这是因为上述警告)

WARNING: Job died due to SIGSEGV - Invalid memory reference

我没有写整个头文件,因为它很长并且会造成困惑,但它是它的地址:http://snap.stanford.edu/snap/doc/snapuser-ref/dd/d90/unicode_8h_source.html在这里,我编写了使用 TUcdFileReader 的头文件的较大部分:

protected:

class TUcdFileReader
{
protected:
TChA buf;
public:
TChA comment; // contains '#' and everything after it
protected:
FILE *f;
int putBackCh;
int GetCh() {
if (putBackCh >= 0) { int c = putBackCh; putBackCh = EOF; return c; }
return fgetc(f); }
void PutBack(int c) { Assert(putBackCh == EOF); putBackCh = c; }
// Returns 'false' iff the EOF was encountered before anything was read.
bool ReadNextLine() {
buf.Clr(); comment.Clr();
bool inComment = false, first = true;
while (true) {
int c = GetCh();
if (c == EOF) return ! first;
else if (c == 13) {
c = GetCh(); if (c != 10) PutBack(c);
return true; }
else if (c == 10) return true;
else if (c == '#') inComment = true;
if (! inComment) buf += char(c);
else comment += char(c); }
/*first = false;*/}
private:
TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }
TUcdFileReader(const TUcdFileReader& r) { Fail; }
public:
TUcdFileReader() : f(0) { }
TUcdFileReader(const TStr& fileName) : f(0), putBackCh(EOF) { Open(fileName); }
void Open(const TStr& fileName) { Close(); f = fopen(fileName.CStr(), "rt"); IAssertR(f, fileName); putBackCh = EOF; }
void Close() { putBackCh = EOF; if (f) { fclose(f); f = 0; }}
~TUcdFileReader() { Close(); }
bool GetNextLine(TStrV& dest) {
dest.Clr();
while (true) {
if (! ReadNextLine()) return false;
TStr line = buf; line.ToTrunc();
if (line.Len() <= 0) continue;
line.SplitOnAllCh(';', dest, false);
for (int i = 0; i < dest.Len(); i++) dest[i].ToTrunc();
return true; }}
static int ParseCodePoint(const TStr& s) {
int c; bool ok = s.IsHexInt(true, 0, 0x10ffff, c); IAssertR(ok, s); return c; }
static void ParseCodePointList(const TStr& s, TIntV& dest, bool ClrDestP = true) { // space-separated list
if (ClrDestP) dest.Clr();
TStrV parts; s.SplitOnWs(parts);
for (int i = 0; i < parts.Len(); i++) {
int c; bool ok = parts[i].IsHexInt(true, 0, 0x10ffff, c); IAssertR(ok, s);
dest.Add(c); } }
static void ParseCodePointRange(const TStr& s, int& from, int &to) { // xxxx or xxxx..yyyy
int i = s.SearchStr(".."); if (i < 0) { from = ParseCodePoint(s); to = from; return; }
from = ParseCodePoint(s.GetSubStr(0, i - 1));
to = ParseCodePoint(s.GetSubStr(i + 2, s.Len() - 1)); }
};

此外,TUcdFileReader 唯一被调用的地方:

class TSubcatHelper
{
public:
bool hasCat; TUniChSubCategory subCat;
TStrH invalidCatCodes;
TUniChDb &owner;

TSubcatHelper(TUniChDb &owner_) : owner(owner_) { }

void ProcessComment(TUniChDb::TUcdFileReader &reader)
{
....

最佳答案

TUcdFileReader被明确定义为具有赋值或复制构造函数,即

private:
TUcdFileReader& operator = (const TUcdFileReader& r)
{ Fail; return *((TUcdFileReader *) 0); }
TUcdFileReader(const TUcdFileReader& r) { Fail; }

看起来他们注定要失败。

在代码中的某处调用它们 - 如果将它们替换为:

private:
TUcdFileReader& operator = (const TUcdFileReader& r); //**not implemented**
TUcdFileReader(const TUcdFileReader& r); //**not implemented**

然后你会得到一个链接器错误。最好的解决办法是(如果可能的话)实现它们。

关于c++ - C++ 头文件中空引用驱动的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com