Hello everyone!
I've faced some weird stuff about STL containers - vectors and maps. My program is reading the binary file, that consist of data chunks. For every chunk object in memory is created and added to the vector (also I've tried maps). Like this:
while ( nCurrentOffset < nItemEndOffset ) {
Chunk::ReadHeader( pInputFile, &pChunkHeader ); m_Chunks.push_back( new Chunk( pChunkHeader, this->m_pHeader->Type ) );
}
During this cycle I'm waiting that the container will hold different pointers to Chunk objects. But every time there are just copies of the last one added.
E.g. if I have 4 chunks - HEDR, CNAM, MAST, DATA in the input file, after the cycle there are 4 DATAs inside the container in place of { HEDR, CNAM, MAST, DATA }. After 1 iteration there is HEDR, after second - 2 CNAMs, etc.
I've tried to add there the objects, not pointers, tried to use maps in place of vectors. Nothing helps. I think that vector::push_back(...) should add copy of object inside the vector, but it seems that it never happens.
Please help, I'm picking with it for a few past days and still can't find any solution.
Update
Thank you a lot for answering! I wasn't thinking about header, and now I see that bug maybe there. I will try to check it and post here when get some results. Anyway here is the code relating to chunk headers. And chunk contains its header and char ptr as binary data array.
#pragma pack ( push, 1 )
struct ChunkHeader
{
long Type;
short Size;
};
#pragma pack ( pop )
long Chunk::ReadHeader( File* pInputFile, ChunkHeader **ppHeader )
{
long nSize = 0;
void *pBuffer = malloc( Chunk::szHeader );
nSize += pInputFile->Read( pBuffer, Chunk::szHeader );
Chunk::ParseHeader( pBuffer, ppHeader );
free( pBuffer );
pBuffer = 0;
return nSize;
}
void Chunk::ParseHeader( void *pBuffer, ChunkHeader **ppHeader )
{
(*ppHeader)->Type = ((long *)pBuffer)[0];
(*ppHeader)->Size = (short)((long *)pBuffer)[1];
}