0

我有以下课程:

class clsJoinFeeder
{
public:
    vector<vector<unsigned char> > UnsignedChars;
};

现在我想在类中添加一个无符号字符向量。我试着这样做:

void CBinLoader::LoadJoinFeaturesFromCompiledDat(clsJoinFeeder &uJoinFeeder)
{
    for (unsigned int y=0;y<2;y++)
    {
        for (unsigned int x=0;x<17;x++)
        {
            pLoadSmallFloatMap(uJoinFeeder);
            pLoadBigUnsignedCharMap(uJoinFeeder);
        }
    }
}

void CBinLoader::pLoadBigUnsignedCharMap(clsJoinFeeder &uJoinFeeder)
{
    unsigned int iItemsCount = 0;
    fread(&iItemsCount,sizeof(unsigned int),1,m_infile);

    unsigned long iByteSizeActualData = 0;
    fread(&iByteSizeActualData,sizeof(unsigned long),1,m_infile);

    vector<unsigned char>nChars;
    nChars.resize(iItemsCount+1);

    fread(&nChars[0],iByteSizeActualData,1,m_infile); //works fine. nChars is correctly filled

    uJoinFeeder.UnsignedChars.push_back(nChars); //here the crash occurs
}

它编译得很好,但在运行时它崩溃并在行中出现堆分配错误:

uJoinFeeder.UnsignedChars.push_back(nChars);

我能做些什么来解决这个问题?我什至没有看到我的错误。

编辑:

感谢您的帮助!正如 Kühl 先生所说,“iByteSizeActualData”是错误的。

4

2 回答 2

1

我猜想iItemsCount并且iByteSizeActualData具有不同的值,后者大于iItemsCount. 就个人而言,我会调用fread()

if (fread(&nChars[0], nChars.size(), 1, m_infile) != nChars.size()) {
    dealWithTheError();
}

(好吧,我不会打电话fread(),但这stream.read()是另一回事)。

于 2013-10-29T22:39:31.030 回答
0

尝试这个:

myJoinFeeder.UnsignedChars.push_back(std::vector<unsigned char>(std::begin(nChars), std::end(nChars));
于 2013-10-29T22:38:00.333 回答