Forum Stats

  • 3,757,576 Users
  • 2,251,248 Discussions


comparison function returns incorrect data

3333809 Member Posts: 15
edited Nov 1, 2016 8:59PM in Berkeley DB

i have modified the examples/c/ex_heap.c to read from one db and insert/delete in another.  For some reason one of the keys that the comparison function receives is either truncated or doesn't match any records in the db.  This is my comparison function

int compare_cmid(dbp, a, b, lob)

        DB *dbp;

        const DBT *a, *b;

        size_t *lob;


       //char* temp_a = (char*)a->data;

       //char* temp_b = (char*)b->data;

       //printf("temp_a = %s\n", temp_a);

       //printf("temp_b = %s\n", temp_b);

        char key_a[25];

        char key_b[25];



       int ascii_a[256];

       int ascii_b[256];

       int i;

       memset(ascii_a, 0, sizeof(ascii_a));

       memset(ascii_b, 0, sizeof(ascii_b));

       strcpy(key_a, a->data);

       strcpy(key_b, b->data);

       printf("key_a = %s\n", key_a);

       printf("key_b = %s\n", key_b);

       int l_a;

       l_a =  strlen(key_a);

       int l_b;

       l_b = strlen(key_b);

       int value;

       for (i=0; i<l_a; i++) {

         value = key_a[i];



       for(i=0; i<l_b; i++) {

         value = key_b[i];



       for(i=0; i<256; i++) {

         if(ascii_a[i] > ascii_b[i]) {

             return 1;

         } else if (ascii_a[i] < ascii_b[i]) {

             return -1;



       return 0;


b is often messed up.  I'm setting the comparison function before opening the db and it gets called fine but just not with the expected keys.  Please assist.

Best Answer

  • userBDBDMS-Oracle
    userBDBDMS-Oracle Member Posts: 783 Employee
    edited Oct 21, 2016 5:40PM Accepted Answer

    What we can do is run the test example that we distribute and make sure that it works correctly and there are no issues.   As a high level note, it is possible that the code changes you made are ok but that the data in the db is corrupted.  I would suggest doing a db_verify to make sure the db is all good.



This discussion has been closed.