STL容器的怪异行为 [英] Weird behaviour of the STL containers

查看:88
本文介绍了STL容器的怪异行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我已经遇到了一些有关STL容器的怪异事物-矢量和地图.我的程序正在读取由数据块组成的二进制文件.对于内存中的每个块对象都会创建并添加到向量中(我也尝试过映射).像这样:

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 ) );
}


在此循环中,我等待容器容纳指向Chunk对象的不同指针.但是每次都只添加最后一个副本.

例如.如果我有4个块-输入文件中的HEDR,CNAM,MAST,DATA,则在循环之后,容器内部有4个DATA来代替{HEDR,CNAM,MAST,DATA}. 1次迭代后出现HEDR,第二秒之后出现2个CNAM,等等.

我尝试在其中添加对象,而不是指针,并尝试使用地图代替矢量.没有任何帮助.我认为vector :: push_back(...)应该在vector内添加对象的副本,但似乎永远不会发生.

请帮忙,我过去几天都在使用它,但仍然找不到任何解决方案.

更新
非常感谢您的回答!我没有考虑过标头,现在我看到了那个错误.我将尝试检查它,并在获得一些结果时在此处发布.无论如何,这里是与块头有关的代码.块包含其标头和char ptr作为二进制数据数组.


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
{
	// 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];
}

推荐答案

我不知道"Chunk"对象是什么,或者构造函数使用参数做什么.但是,从您的描述来看,听起来好像有人在保存指针"pChunkHeader"的值",而不是复制它或其指向的内容.

症状是,向量中的所有内容都是读入的最后一个内容的副本,通常是保存指向更改数据的指针,以便仅记住最后写入的内容.

如果您发布更多的相关代码,例如Chunk构造函数和"ReadHeader()"的功能以及pChunkHeader的定义,这将有所帮助.
除了您的对象之外,STL可能根本没有问题.
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.


这篇关于STL容器的怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆