Thread: List Problem-Last entry overwriting all others

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    29

    List Problem-Last entry overwriting all others

    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();

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You can't say "array1 = array2" and expect all the elements to be copied automagically. What it actually does it set the address of array1 to be the address of array2. So when you set a whole bunch of node_id's to be equal to "temporary", you are making them all point to the same location in memory. That's why it looks like they're being overwritten.

    To copy one string into another, use strcpy.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    29
    Thanks, I used memcpy() and it solved it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overwriting problem in IRC game bot
    By tvwenger in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2010, 08:53 PM
  2. find entry in linked list
    By fcommisso in forum C Programming
    Replies: 4
    Last Post: 11-30-2009, 01:14 AM
  3. overwriting hash entry
    By Cpro in forum C++ Programming
    Replies: 0
    Last Post: 03-21-2008, 07:13 PM
  4. how to avoid duplicate entry to a list/array?
    By janetsmith in forum C Programming
    Replies: 5
    Last Post: 12-17-2006, 09:32 AM
  5. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM