LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ListFindItem with StartingPosition out of range: BUG

Solved!
Go to solution

Hello,

 

The index returned by ListFindItem is WRONG when:

  • StartingPosition  = 0 ( this is a wrong value: StartingPosition must be >=1) .
  • The searched value equal to the LAST item inserted in the List:

The code example below shows the bug in the TEST_D:

 

int main (int argc, char *argv[])
{
	ssize_t 	startingPosition = 0;
	size_t 		index = 0;
	int 		val = 0;

	ListType myList = NULL;
	
	myList = ListCreate (sizeof(int));
	
	val = 1;
	ListInsertItem (myList, &val, END_OF_LIST);
	
	val = 2;
	ListInsertItem (myList, &val, END_OF_LIST);
	
	val = 3;
	ListInsertItem (myList, &val, END_OF_LIST);
	
	// TEST_A: OK
	startingPosition = 1;
	val = 1;
	index = ListFindItem (myList, &val, startingPosition, IntCompare);
	// index = 1 --> OK

	// TEST_B: OK
	startingPosition = 10; // WRONG value: startingPosition is over the number of items.
	val = 1;   
	index = ListFindItem (myList, &val, startingPosition, IntCompare);
	// index = 0 --> OK because startingPosition is out of range (>3)
	
	// TEST_C: OK
	startingPosition = 0; // WRONG value: startingPosition must be >= 1
	val = 1;
	index = ListFindItem (myList, &val, startingPosition, IntCompare);
	// index = 0 --> OK because startingPosition is out of range (<1)  
	
	// TEST_D: NOT OK
	startingPosition = 0; // WRONG value: startingPosition must be >= 1
	val = 3;   // value of the LAST ITEM inserted in the List
	index = ListFindItem (myList, &val, startingPosition, IntCompare);
	// index = 3 --> NOT OK: the behaviour is different compare to TEST_C. 
	// In this case, ListFindItem must return 0 (NOT 3) because startingPosition is out of range (<1).
	// If you look for another item (not the last: val=1 ou val=2) ListFinfItem returns 0 --> OK
	return 0;
}

 

 

 

0 Kudos
Message 1 of 4
(4,285 Views)
Solution
Accepted by topic author jm73

Hello jm73,

 

The behavior you are seeing is expected. Please note, you can use the following two macros to specify starting position: END_OF_LIST and FRONT_OF_LIST. The macros are defined as follows in toolbox.h:

 

#define FRONT_OF_LIST -1 /* Handy Constants that substitute for item positions */
#define END_OF_LIST 0 /* END_OF_LIST means one past current length of list when
inserting. Otherwise it refers the last item in the list. */

 

By specifying 0, you are telling the ListFindItem function to start looking at the end of the list, which is why it finds the last item.

Daniel Dorroh
National Instruments
Message 2 of 4
(4,271 Views)

The reason this is not a bug is because passing 0 to ListFindItem is actually not invalid. This happens to be the special value END_OF_LIST:

 

list.png

 

Given this, your results in TEST_C and TEST_D make sense.

 

Luis

0 Kudos
Message 3 of 4
(4,269 Views)

Darn, took too long...

0 Kudos
Message 4 of 4
(4,268 Views)