Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!




0 = 1 and i don't know why

 
Reply to this topicStart new topic

0 = 1 and i don't know why

Ambient
3 Dec, 2006 - 08:43 PM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 1


My Contributions
brief explanation:

this is a test i'm given

the compare function passed by parameter in main is the same as compare_fn

i am returning 0 from the end of one function, and when i go back into the otherone, the expression evaluates to 1, and i don't know why



CODE

int compare(void *x, void *y) {
  long sub= (long) x - (long) y;
  if (sub > 0)
    return 1;
  else
    if (sub < 0)
      return -1;
    else return 0;
}

int main() {
  Table table;
  long key_i= 212, value_i= 2 * 212;
  void *value;

  /*
   *  We pass NULL as delete functions, because we will not use dynamic memory
   *  for key or value fields.
   */
  table= create(compare, NULL, NULL);

  printf("Created a table. Size is %d.\n", size(table));

  printf("Inserting item with key %li and value %li\n", key_i, value_i);
  insert(table, (void *) key_i, (void *) value_i);
  printf("Size is %d.\n\n", size(table));

  printf("Deleting the single item of the table.\n\n");
  delete(table, (void *) key_i);

  printf("Searching for item with key %li ...\n\t", key_i);
  value= lookup(table, (void *) key_i);
  if (value != NULL)
    printf("Retrieved the item with key %li, its value is: %li\n",
           key_i, (long) value);
  else printf("Item with key %li does not exist in the table\n", key_i);

  printf("Size is %d.\n", size(table));

  clear(table);
  free(table);

  return 0;
}


Starting program: --------------------
Created a table. Size is 0.
Inserting item with key 212 and value 424
Size is 1.

Deleting the single item of the table.


Breakpoint 1, delete (table=0x502010, key=0xd4) at bst.c:197
warning: Source file is more recent than executable.

197       if (table == NULL)
(gdb) next
200       node= ((BST_node *)((long)(table->table)));
(gdb) next
201       link= &node;
(gdb) next
204       while (node != NULL) {
(gdb) next
206         if (table->compare_fn(key,(node->key)) != 0) {
(gdb) step
compare (x=0xd4, y=0xd4) at public03.c:20
20        long sub= (long) x - (long) y;
(gdb) display sub
1: sub = 0
(gdb) next
21        if (sub > 0)
1: sub = 0
(gdb) next
24          if (sub < 0)
1: sub = 0
(gdb) next
26          else return 0;
1: sub = 0
(gdb) next
27      }
1: sub = 0
(gdb) next
delete (table=0x502010, key=0xd4) at bst.c:212
212         if (node->right != NULL) {
(gdb) display table->compare_fn(key,(node->key))
2: table->compare_fn (key, node->key) = 1
(gdb)      

User is offlineProfile CardPM
+Quote Post

Xing
RE: 0 = 1 And I Don't Know Why
3 Dec, 2006 - 09:06 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Since you didn't provided the complete code So I am unable to compile it. But by seeing your compare code, I can see a problem.
I think long sub= (long) x - (long) y; should be long sub= (*(long*) x - *(long*) y);
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:11PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month