Thread: Does bsearch ever miss?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    96

    Does bsearch ever miss?

    I've got 2 lists that I'm comparing (attached).
    If I do a straight strcmp it shows a match which bsearch is not finding.

    I've attached the code file. It's a bit complex. It's a derivitive of something that Salem wrote for me.

    It's missing on
    hornypotter.com and on
    www.herbsbuy.com
    It finds 8 out of 10 and I can't figure out for the life of me why it consistently misses on those 2.
    Again, a straight strcmp shows a match in the x loop below.
    the bsearch right below it misses on those 2.

    The function in question is main. It is intended to be used as a dll.
    The problem is in the code starting on line: 272.

    You can see the problem by running it and seeing that it deletes 8 records from UTest.txt instead of the 10 that it should.

    Any ideas as to what the problem might be?
    Does bsearch provide inconsistent results?

    The commandline run should be SLSSeoUrlFunc MTest.txt UTest.txt UUTest.txt

    Code:
    //Shows a match on the 2 URLs in question.
    	for ( int i = 0 ; i < nUpdate ; i++ ) 
    	{
    		for ( int x = 0 ; x < nUnknown ; x++ ) 
    		{
    			printf("\n%s : %s : %d", dbUpdate[i].base_url, dUnknown[x].base_url, strcmp(dbUpdate[i].base_url, dUnknown[x].base_url));
    		}	
    			
    	}
    	//Misses on the 2 URLs in question.
    	for ( int i = 0 ; i < nUpdate ; i++ ) 
    	{
    		db_st *f = bsearch(&dbUpdate[i], dUnknown, nUnknown, sizeof(*dUnknown), sortfn );
    		if ( f ) 
    		{
    			strcpy((*f).base_url,"");
    		}		
    	}
    Last edited by MAtkins; 02-16-2011 at 08:59 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you're failing on a string search, the usual suspects include:

    1) One string has a newline char on the end (fgets() puts one there), and the other doesn't (fgets doesn't put the newline on it, if there isn't room for one).

    2) One string has an end of string char, the other "string" is just a bunch of letters, w/o an end of string char \0' at the end.

    3) The data is goofed - one string somehow has an extra space two underline char's, that looked like one, or one string has a letter from another language, like u with two dots above it, (German), instead of just a u, a n with the tilde stripped off (Spanish), etc. One string has a space ' ' char added to it.

    On the second Wednesday of each month, we set a random char to fail in binsearch -- lots of panic and fun to watch.

    Yes, binsearch always works, but the list much be sorted according to ascii values, NOT lexicographically (like a dictionary). There are differences.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    Well, I figured out that the problem was *not* bsearch (whew!).

    Strange though, I don't know why this is:
    It had to do with setting the base_url to "".
    I guess it's one of those 'undefined behavior' situations.

    Well, it's solved now . . .

    Code:
    for ( int i = 0 ; i < nUpdate ; i++ ) 
    	{
    		f = bsearch(&dbUpdate[i], dUnknown, nUnknown, sizeof(*dUnknown), sortfn );
    		if ( f ) 
    		{
    			//strcpy((*f).base_url, "") //<- this caused the problem - I have no idea how.
    			(*f).DomainFound = -1; //This was my work-around 
    			//f = NULL; //This did NOT help the problem
    		}		
    	}

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I have found bsearch to go flakey in the case where the count of elements is zero. It blew up royal. I had to code a check for that instead of expecting the function to return NULL. Sorry I don't remember the compiler I was using at the time but it was late 90s.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    			//strcpy((*f).base_url, "") //<- this caused the problem - I have no idea how.
    is base field char pointer? Did you allocate memory?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bsearch problem
    By hawaiian robots in forum C Programming
    Replies: 5
    Last Post: 04-06-2010, 10:19 AM
  2. Nostalgy
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 67
    Last Post: 07-08-2006, 06:08 PM
  3. bsearch( ) and qsort( ) not agreeing
    By Thumper333 in forum C Programming
    Replies: 1
    Last Post: 10-24-2004, 09:56 PM
  4. Problem Using bsearch
    By Zildjian in forum C Programming
    Replies: 4
    Last Post: 11-13-2003, 08:14 PM
  5. bsearch
    By Kev2 in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 08:56 PM