Reputation: 11399
I have the following class:
class clsJoinFeeder
{
public:
vector<vector<unsigned char> > UnsignedChars;
};
Now I would like to add a vector of unsigned chars to the class. I tried to do it like this:
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
}
It compiles fine, but at runtime it crashes with a heap alloc error in the line:
uJoinFeeder.UnsignedChars.push_back(nChars);
What can I do to resolve the problem? I don't even see my mistake.
Edit:
Thank you for the help! "iByteSizeActualData" was wrong as Mr. Kühl stated.
Upvotes: 0
Views: 61
Reputation: 153840
I would guess that iItemsCount
and iByteSizeActualData
have different values with the latter being bigger than iItemsCount
. Personally, I would invoke fread()
as
if (fread(&nChars[0], nChars.size(), 1, m_infile) != nChars.size()) {
dealWithTheError();
}
(well, I wouldn't call fread()
but rather stream.read()
but this is a different matter).
Upvotes: 1
Reputation: 3324
Try this:
myJoinFeeder.UnsignedChars.push_back(std::vector<unsigned char>(std::begin(nChars), std::end(nChars));
Upvotes: 0