Kyle
Kyle

Reputation: 217

strange behavior of C++ STL container set

I am having some very strange behavior when using a set (it's actual a 3d vector of sets). I have the following at the end of a loop (the comments are behavior the fourth time the loop occurs, so it's not happenining right away)

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

The definition of particles_celllist, grid_rank_lookup, and grid_locations are:

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

Note: the first is a local variable, while the other 2 are inputs to the function.

I have no idea what is causing this or even how to go about figuring out what is wrong. I see no logical explanation within the confines of the code itself, so maybe it is something to do with the memory? The comments are what happens when I run it on a single processor, but if I do it on 2 processors (it is an mpi code) it seems to happen at a random time on a random processor (I didn't include all the details here because the code is quite long, but I can post it if you feel the information here is not sufficient to guess at what is wrong)

Thanks

Upvotes: 0

Views: 169

Answers (1)

ecatmur
ecatmur

Reputation: 157314

1071644672 has the same value representation as 1.75 (as 32-bit values).

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

is overwriting memory belonging to particles_celllist. Probably grid_locations[box_counter] is insufficiently large to allow grid_locations[box_counter][5] to access memory correctly; try changing it to grid_locations[box_counter].at(5) and see if it breaks.

Upvotes: 1

Related Questions