我有以下课程:
class clsJoinFeeder
{
public:
vector<vector<unsigned char> > UnsignedChars;
};
现在我想在类中添加一个unsigned chars向量。 我试着这样做:
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”错了。
答案 0 :(得分:1)
我猜我会iItemsCount
和iByteSizeActualData
有不同的值,后者大于iItemsCount
。就个人而言,我会调用fread()
作为
if (fread(&nChars[0], nChars.size(), 1, m_infile) != nChars.size()) {
dealWithTheError();
}
(好吧,我不会打电话给fread()
而是stream.read()
,但这是另一回事。)
答案 1 :(得分:0)
试试这个:
myJoinFeeder.UnsignedChars.push_back(std::vector<unsigned char>(std::begin(nChars), std::end(nChars));