Hi,
In the below program I have a list of structs(chordRing.chordNodeIDList). When an entry is added to the end of a list it is somehow overwriting one particular parameter (chordEntry->node_id) in every struct in the list. chordEntry->node_id is a 20 byte char array. I can't see why it would overwrite every entry- it has to be a memory error I assume but I'm stumped as to how to fix it.
Any help would be appreciated.
Code:
intersection_details= (ILS_Intersection_Trajectory_Vals*)op_prg_mem_alloc(sizeof(ILS_Intersection_Trajectory_Vals));
intersectionCoordinates = (WsqT_Location_Message *) op_prg_mem_alloc (sizeof (WsqT_Location_Message));
for (count = 0; count < sizeOfIntersectionList; count++)
{
intersection_details = (ILS_Intersection_Trajectory_Vals*) op_prg_list_access (intersection_ptr->intersection_list, count);
//If the Chord ring doesn't exist, this will create a list and add the node ID to it
if (chordRing.chordNodeIDList == OPC_NIL)
{
chordEntry = (DhtT_Chord_Entry*) op_prg_mem_alloc(sizeof(DhtT_Chord_Entry));
chordEntry->ip_addr = intersection_details->int_ip;
if (cantor_sfc == OPC_TRUE)
{
intID = Algorithm_Cantor_XY_to_N (intersection_details->x_pos, intersection_details->y_pos);
//tmp_num = intID/20;
dblNodeID = (double)intID;
dbl_tmp_num = dblNodeID/20.0;
tmp_num = ceil(dbl_tmp_num);
for (i=0; i<=19;i++)
{
tmp [i]=(char)tmp_num;
if(i== 19)
{
possible_difference = intID - (tmp_num * 19);
tmp[i] = (char)possible_difference;
}
}
temporary = (DhtT_Key_Bytes) op_prg_mem_alloc(20);
temporary = (DhtT_Key_Bytes)tmp;
chordEntry->node_id = temporary;
}
else
{
chordEntry->node_id = dht_get_node_id(intersection_details->int_ip);
}
chordEntry->x_pos = intersection_details->x_pos;
chordEntry->y_pos = intersection_details->y_pos;
intID = sha_get_int_id(chordEntry->node_id);
printf ("\nInt id: %lf",intID);
//global_chord_ring is a static instance of a List
chordRing.chordNodeIDList = op_prg_list_create();
//insert the node ID as the first and only entry currently in the list.
//op_prg_list_insert(chordRing.chordNodeIDList, *myNodeID, OPC_LISTPOS_HEAD);
//new
op_prg_list_insert(chordRing.chordNodeIDList, chordEntry, OPC_LISTPOS_HEAD);
printChordRing();
if ((op_prg_odb_ltrace_active ("trace_chord") == OPC_TRUE))
{
printf("\nCreating Chord Ring for the first time");
}
}
else //The Chord Ring does exist so just insert my Node ID in a sorted position
{
//Insert the current entry into sorted position. The list is sorted from lowest to highest
// DHT ID, like a Chord ring that is broken and stretched into a line.
chordEntry = (DhtT_Chord_Entry*) op_prg_mem_alloc(sizeof(DhtT_Chord_Entry));
chordEntry->ip_addr = intersection_details->int_ip;
if (cantor_sfc == OPC_TRUE)
{
intID = Algorithm_Cantor_XY_to_N (intersection_details->x_pos, intersection_details->y_pos);
tmp_num = intID/20;
for (i=0; i<=19;i++)
{
tmp [i]=(char)tmp_num;
if(i== 19)
{
possible_difference = intID - (tmp_num * 19);
if (possible_difference > tmp_num)
tmp[i] = (char)possible_difference;
}
}
temporary = (DhtT_Key_Bytes) op_prg_mem_alloc(20);
temporary = (DhtT_Key_Bytes)tmp;
chordEntry->node_id = temporary;
}
else
{
chordEntry->node_id = dht_get_node_id(intersection_details->int_ip);
}
intnodeID = sha_get_int_id(chordEntry->node_id);
chordEntry->x_pos = intersection_details->x_pos;
chordEntry->y_pos = intersection_details->y_pos;
printf ("\nInt id: %lf",intnodeID);
op_prg_list_insert_sorted(chordRing.chordNodeIDList, chordEntry, dht_sup_chord_ring_entry_compare);
//op_prg_list_insert(chordRing.chordNodeIDList, chordEntry, OPC_LISTPOS_TAIL);
printChordRing();
}
}
printChordRing();