Click here to Skip to main content
14,875,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 )    // Parse header item
   Chunk::ReadHeader( pInputFile, &pChunkHeader );   // Read chunk's header
   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.

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
	// Chunk's type
	long	Type;
	// Chunk's data size
	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];
Updated 17-Feb-12 15:06pm
Philippe Mori 6-Jun-16 19:37pm
As seen by your updated code, clearly your code is wrong as it seems that you always load data in the same "chunk header"... Usually, your struct would be on the stack and you would copy it by value into the vector.
Philippe Mori 6-Jun-16 19:45pm
And why are you using malloc and free in C++ code?

In fact, why are you dynamically allocate that block insteaf of putting it on the stack. If the header data so big?

And it is essentially useless to set pBuffer to 0 as it will be out of scope.

You should also declare your variable at their first use.

Finally, why are you using a pointer to a pointer for the header?

The way the code is written look more like C code than C++ code.
Philippe Mori 6-Jun-16 19:47pm
There is nothing wrong with STL! The problem is that you essentially store copies of pointers.
Anruin 7-Jun-16 3:25am
Hi, thanks for answers, but this question is quite old, so even this code is lost somewhere in history :) My C/C++ knowledge was not good at that time, and I hope this code and your answers will be useful to someone like me 3 years ago.

1 solution

I have no idea what the "Chunk" object is or what the constructor does with the parameters. However, from your description, it sounds like somebody's saving the "value" of the pointer "pChunkHeader" rather than copying it or the contents it points to.

The symptom, the things in your vector are all copies of the last thing your read in is typical of saving a pointer to changing data so that only the last thing written is remembered.

It would help if you posted more of the relevant code, like the Chunk constructor and what "ReadHeader()" does and the definition of pChunkHeader.

There may not be a problem with STL at all but with your object.
Anruin 17-Feb-12 21:11pm
Ah, I've forgot to reinitialize pChunkHeader on every cycle iteration. Thanks you a lot again for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900