I was facing some problems while using vector in one of my simulation code. I
wanted to store structure in a vector. The structures are added with push_back
method of vector. But after adding few elements, it corrupts some of the
elements.
\\Global declaration
struct neighborRecord {
int nId;
bool type;
double locX;
double locY;
double rssi;
double dist;
};
vector<neighborRecord> neighborTable;
\\Need to call this funtion. All arguments are passed properly in the function
updateNeighborTable(int nId, bool A, double fromX, double fromY, double rss,
double Distance)
{
int i = 0,pos = -1;
int tblSize = (int)neighborTable.size();
for (i = 0; i < tblSize; i++){
if (neighborTable[i].nId == nId){
pos = i;
break;
}
}
\\ If a element with a nId is not present then add new record
if (pos == -1){
neighborRecord newRec;
newRec.nId = nId;
newRec.type = fromAnchor;
newRec.locX = fromX;
newRec.locY = fromY;
newRec.rssi = rss;
newRec.dist = Distance;
//neighborTable.reserve(sizeof(newRec));
neighborTable.push_back(newRec);
for (i=0; i< (int)neighborTable.size(); i++) \\ Just to check
print
cout << "nId: "<<neighborTable[i].nId<< "type:" <<
neighborTable[i].type;
}
\\else if element with argument nId is present in the vector then update that
record
else{
neighborTable[pos].nId = nId;
neighborTable[pos].type = fromAnchor;
neighborTable[pos].locX = fromX;
neighborTable[pos].locY = fromY;
neighborTable[pos].rssi = rss;
neighborTable[pos].dist = Distance;
for (i=0; i< (int)neighborTable.size(); i++) \\ Just to check
print
cout<< "nId: "<<neighborTable[i].nId<< "type:" <<
neighborTable[i].type;
}
}
This is a code snippet from my simulator code. Actual code is very long. I
tried to run similar code as separate small CC program, it works perfectly but
doesnot work in the simulator code. Whenever I print the vector contents, after
10-12 records, it starts corrupting the structure data stored in vector.
Example trace output of the simulator
1.342766435621 SN.node[6].Application nId: 13type:1
1.342766435621 SN.node[6].Application nId: 8type:1
1.342766435621 SN.node[6].Application nId: 28type:1
then it becomes
2.518941580248 SN.node[6].Application nId: 13type:1
2.518941580248 SN.node[6].Application nId: -2132507471type:47
2.518941580248 SN.node[6].Application nId: 28type:1
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/6001b03d-ddef-4afc-bd0a-34ad85d279c4%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.