Probably there is an error in some other code. Somewhere you store the data beyond the allocated area of some other global variable and the address you use happens to be just inside this global vector. This kind of bugs is rather hard to find. Setting watchpoints in debugger (if corrupted element index is always the same) or running your program under Valgrind may help.
2013/11/21 VUkani <[email protected]>: > The nId is a node Id. There are only 50 nodes in the simulation. So nId can > not be greater than 50. Initially I set it as unsigned short but later > changed it to int. > > On Thursday, November 21, 2013 12:37:34 AM UTC+5:30, Lucifer wrote: >> On Thursday, 21 November 2013 00:26:17 UTC+5:30, VUkani wrote: >> > 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 >> ---------------------- >> does the nID value fit in within integer limits ? did u check ? > > -- > 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/819106b2-5389-40a0-84d5-c49624872a2c%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. -- 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/CANBUrM9m%2BN89Oe6MViYHq4NCtXc7X_BCCzsU_yNo%2BT7WDyXh3Q%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
