bsearch problem

This is a discussion on bsearch problem within the C Programming forums, part of the General Programming Boards category; Here's what I'm trying to do. I have a char pointer that contains a pagefile size of records in it ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    23

    bsearch problem

    Here's what I'm trying to do. I have a char pointer that contains a pagefile size of records in it that I've read from disk. This file is a binary file. From this I am trying to bsearch the pointer.

    So, in essence each record in the file is 50 bytes long, with a pageFile being read in at a time.

    my bsearch is as follows, where sid is the id that I'm searching on and dest is the char pointer with 50 * pageSize bytes of data:

    bsearch(&sid, dest, pageSize, 50, comparator);

    this is my comparator function:
    Code:
    unsigned int comparator(const void* a, const void* b)
    {
       char* ca = (char*)a;
       char* cb = (char*)b;
    
       unsigned int ta = 0;
       unsigned int tb = 0;
    
       /* the 16th element is where the id starts, thats what I'm 
           searching on */
       memcpy(&ta, &(ca[16]), sizeof(unsigned int));
       memcpy(&tb, &(cb[16]), sizeof(unsigned int));
    
       return (ta - tb);
    }
    I have verified that the data is complete, but for some reason, there is garbage data in ca, but in cb the correct data is present. Even more bizarre is I'm not getting any segfaults to indicate that there is in fact garbage data about.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Disregard this. I was tired of trying to debug it so I just whipped up my own binary search.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Was sid an array of 50 bytes, with an int to search for at byte 16?

    Was the array previously sorted using say qsort and the same comparison function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    No. sid is an unsigned int that i'm searching for in the dest character array (which is 50 * pageSize long where pageSize is one of 100, 1000, or 10000). I didn't use qsort as we were not permitted to.

    I tried various methods including copying the data to a struct and passing an array of structs in, but it all resulted in the same garbage data in the first variable, but the correct data in the second variable.

    In the end I just implemented my own binary search function, that performs fairly fast. I'll come back to this after i hand in my assignment but for know, as long as it works i'm gonna go with my implementation.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, that would be the problem then. sid needs to be an object that you're looking for (and you're looking for 50-byte objects with the key at byte 16, not ints).

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Quote Originally Posted by tabstop View Post
    Well, that would be the problem then. sid needs to be an object that you're looking for (and you're looking for 50-byte objects with the key at byte 16, not ints).
    Thanks. This was the first time using bsearch, I should of done more research.

    I was under the assumption that the data passed in the second argument was the data that was being passed as both param1 and param2 in the comparator function. Silly me, the first param is actually the key were searching on. Do'h!

    This following change fixed it:

    Code:
    unsigned int comparator(const void* a, const void* b)
    {
       int* ia = (int*)a;
       char* cb = (char *)b;
       int ib;
    
       memcpy(&ib, &(cb[16]), sizeof(unsigned int));
    
       return (*ia) - ib;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21