C ++ STL容器集的奇怪行为

时间:2012-08-17 20:25:51

标签: c++ stl set

我在使用集合时有一些非常奇怪的行为(它实际上是一组3d矢量集)。我在循环结束时有以下内容(注释是第四次循环发生时的行为,所以它不会马上发生)

cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl; 
//returns 4

grid_locations[box_counter][0]=i*xgridlength;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl; 
//returns 4

grid_locations[box_counter][1]=(i+1)*xgridlength;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl; 
//returns 4

grid_locations[box_counter][2]=j*ygridlength;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl; 
//returns 4

grid_locations[box_counter][3]=(j+1)*ygridlength;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl; 
//returns 4

grid_locations[box_counter][4]=k*zgridlength;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl; 
//returns 4

grid_locations[box_counter][5]=(k+1)*zgridlength;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl;
// returns 1071644672

grid_rank_lookup[box_counter]=use_rank;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl;
// returns 1071644672

box_counter++;
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl;
// returns 1071644672

use_rank=(use_rank+1)%world.size();
cout<<"size of set at 3 0 0 is "<<particles_celllist.at(3).at(0).at(0).size()<<endl;
// returns 1071644672

particles_celllistgrid_rank_lookupgrid_locations的定义是:

vector<vector<vector<set<int> > > > particles_celllist;
map<int,vector<double> > grid_locations
map<int,int> grid_rank_lookup

注意:第一个是局部变量,而另外两个是函数的输入。

我不知道造成这种情况的原因是什么,甚至不知道如何找出问题所在。我认为在代码本身的范围内没有逻辑解释,所以也许它与内存有关?评论是我在单个处理器上运行时会发生什么,但如果我在2个处理器上运行它(它是一个mpi代码),它似乎是在随机处理器上随机发生的(我没有包括所有细节)这里因为代码很长,但如果你觉得这里的信息不足以猜出错误的话,我可以发帖。

由于

1 个答案:

答案 0 :(得分:1)

1071644672具有与1.75相同的值表示(作为32位值)。

grid_locations[box_counter][5]=(k+1)*zgridlength;

覆盖属于particles_celllist的内存。可能grid_locations[box_counter]不足以允许grid_locations[box_counter][5]正确访问内存;尝试将其更改为grid_locations[box_counter].at(5)并查看是否中断。