GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jun-2007, 21:23
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 469
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Linked list: differing results on same code.


hi again,
Someone from the list sent me a linked list example they had made.
When I compile/run the output shows a problem.
When he runs it it doesn't.
Even when I run the same .exe as he And vise versa!
I am win98/mingw gnu c (or dmc) , , He is XP devc++...
I hate to bother the list but we are baffled and wondered if someone might
kindly offer some reason for this anomaly.
Here is the exact code we both are using: (hope I don't get kicked off for all this)
CPP / C++ / C Code:
#include <stdio.h>
#include<stdlib.h>         /* for malloc()                        */

typedef struct Linked_List{
        int value;
        struct Linked_List *next_item;
        } Linked_List;

void get_new_number(Linked_List *head);            /* get a number and call insert element to put in list, is passed address of head */
void insert_element(int input, Linked_List *head); /* get an input and head of the list insert element at next free location by allocation */
void print_list(Linked_List *head);                /* print whole list */
int  menu(Linked_List *head);                       /* menu to choose the different options */
int  find_by_value(Linked_List *head, Linked_List *dummy); /* delete element by value inputed by users */
void delete_element(Linked_List *head);            /* delete an element given the element before it */
void bubble_sort(Linked_List *head);
void swap(Linked_List *swap1);

int main(void)
{
  int menu_return;
  Linked_List *head;                       /* head pointer to list */
  head = malloc(sizeof(Linked_List));      /* malloc list          */
  head->next_item = malloc(sizeof(Linked_List));  /* initialize first element of the list */
  head->next_item->next_item = NULL;       /* set to null to avoid bad dereference */

  printf("\n     Welcome, here you can play around with a linked list. \n\
     Please insert as many numbers as you want: \n\n");

  printf("sizeof(head)= %d (head->next_item)= %d \n", sizeof(*head), sizeof(*head->next_item) );

  while(1){
    menu_return = menu(head);
    if(menu_return == 1) return 0;
      /* system("cls");  works, but I like seeing the data */
  }

  /* system("PAUSE");	overheats my processor on lappy */
  while(getchar() != '\n');
  return 0;
}

/************  Functions  *****************/
int menu(Linked_List *head)
{
  char menu_choice;
  int return_value;

  /* printf(" a - Print the list. \n b - Insert new element. \n c - Delete element by value. \n d - Sort list in ascending order. \n q - To quit.\n");
     replaced for testing with:  */
  printf(" a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit.\n" );
  menu_choice = getchar();

  switch (menu_choice){
    case 'a':
      print_list(head->next_item);
      /* system("PAUSE"); */
      break;
    case 'b':
      get_new_number(head);
      break;
    case 'c':
      return_value = find_by_value(head->next_item, head);
      if(return_value == 1)
        printf("Element destroyed.\n");
      else
        printf("Value not found.\n");
      /* system("PAUSE"); */
      break;
    case 'd':
      bubble_sort(head);
      /* (howard) inserted this here for tesing */
      print_list(head->next_item);
      break;
    case 'q':
      printf("bye. \n");
      /* system("PAUSE"); */
      return 1;
      break;
    default:
      printf("Invalid input.  \n");
      /* system("PAUSE"); */
      break;
  }
  getchar();
  return 0;
}
/************************************************/
void get_new_number(Linked_List *head)
{
  int input;
  char buff[10];
  printf("Enter Number: ");
  if(scanf("%d",&input)){
    insert_element(input, head);
  }
  else{
    gets(buff);
  }
}
/************************************************/
void insert_element(int input, Linked_List *head)
{
  Linked_List *temp;    /* temp pointer used inside this function */
      /* while there is another item next keep going through list */
  while(head->next_item != NULL){
    head = head->next_item;
  }
  temp = malloc(sizeof(Linked_List));         /* create new entry */

  head->next_item = temp;  /* last entry next pointer now points to new item */
  head->value     = input; /* last entry gets the value next pointer value is uninitialised */
  temp->next_item = NULL;  /* last item pointer set to null to avoid bad dereference */
}

/************************************************/
void print_list(Linked_List *head)
{
  int i = 0;
       /* while there is another item next keep going through list */
  while(head->next_item != NULL){
    i++;
    /* printf("%d items value = %d \n",i, head->value);
       replace for tesing with: */
    printf("      item: %d, head= %p, value= %2d, next_item= %p \n", i, (void*)head, head->value, (void*)head->next_item );

    head = head->next_item;
  }
  printf("and the NULL pointer: \n      item: %d, head= %p, value= %2d, next_item= %p \n\n", i, (void*)head, head->value, (void*)head->next_item );
}
/************************************************/
int find_by_value(Linked_List *head, Linked_List *dummy)
{
  int input;
  Linked_List* last_head = dummy;

  printf("Input the value to be found and deleted: \n");
  scanf("%d",&input);

    /* while the value has not be found and the list hasn't ended */
  while(head->value != input && head->next_item != NULL){
    last_head = head;
    head = head->next_item;
  }
  if(head->value == input){
    delete_element(last_head);
    return(1);                 /* if found return 1 */
  }
  else return(0);              /* else return 0 */
}
/************************************************/
/*  this can't delete the first value of the list... */
void delete_element(Linked_List* last_head)
{
  Linked_List* item_to_delete;
  item_to_delete = last_head->next_item;
  last_head->next_item = item_to_delete->next_item;
  free(item_to_delete);
}
/************************************************/
void bubble_sort(Linked_List *head)
{
for(;head->next_item->next_item != NULL; head = head->next_item){
  if(head->next_item->value > head->next_item->next_item->value) /* if the next value is bigger than the first */
    swap(head);                                                  /* swap em by passing the address to the element before both of em */
  }
}
/************************************************/
void swap(Linked_List *head)
{
  Linked_List *temp;

  temp = head->next_item->next_item->next_item;            /* this is the pointer of the second item */
  head->next_item->next_item->next_item = head->next_item; /* replace the second item pointer by first item pointer */
  head->next_item = head->next_item->next_item;            /* make the head point to the second item so it now becomes first */
  head->next_item->next_item->next_item = temp;            /* set the pointer to the new second item to the old value of second item */
}
Here is an illustration of the problem using my output: (his doest have the problem)
Code:
my sample output: sizeof(head)= 8 (head->next_item)= 8 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 3 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 2 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 1 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. a item: 1, head= 006603DC, value= 3, next_item= 006603E8 item: 2, head= 006603E8, value= 2, next_item= 00669640 item: 3, head= 00669640, value= 1, next_item= 0066964C and the NULL pointer: item: 3, head= 0066964C, value= 0, next_item= 00000000 Press any key to continue . . . a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d item: 1, head= 006603E8, value= 2, next_item= 00669640 item: 2, head= 00669640, value= 1, next_item= 0066964C item: 3, head= 0066964C, value= 0, next_item= 006603DC and the NULL pointer: item: 3, head= 006603DC, value= 3, next_item= 00000000 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d item: 1, head= 00669640, value= 1, next_item= 0066964C item: 2, head= 0066964C, value= 0, next_item= 006603E8 item: 3, head= 006603E8, value= 2, next_item= 006603DC and the NULL pointer: item: 3, head= 006603DC, value= 3, next_item= 00000000 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d item: 1, head= 0066964C, value= 0, next_item= 00669640 item: 2, head= 00669640, value= 1, next_item= 006603E8 item: 3, head= 006603E8, value= 2, next_item= 006603DC and the NULL pointer: item: 3, head= 006603DC, value= 3, next_item= 00000000 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 6 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. a item: 1, head= 0066964C, value= 0, next_item= 00669640 item: 2, head= 00669640, value= 1, next_item= 006603E8 item: 3, head= 006603E8, value= 2, next_item= 006603DC item: 4, head= 006603DC, value= 6, next_item= 00669658 and the NULL pointer: item: 4, head= 00669658, value= 0, next_item= 00000000 Press any key to continue . . . a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d item: 1, head= 0066964C, value= 0, next_item= 00669640 item: 2, head= 00669640, value= 1, next_item= 006603E8 item: 3, head= 006603E8, value= 2, next_item= 00669658 item: 4, head= 00669658, value= 0, next_item= 006603DC and the NULL pointer: item: 4, head= 006603DC, value= 6, next_item= 00000000 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 5 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. a item: 1, head= 0066964C, value= 0, next_item= 00669640 item: 2, head= 00669640, value= 1, next_item= 006603E8 item: 3, head= 006603E8, value= 2, next_item= 00669658 item: 4, head= 00669658, value= 0, next_item= 006603DC item: 5, head= 006603DC, value= 5, next_item= 00669664 and the NULL pointer: item: 5, head= 00669664, value= 0, next_item= 00000000 .... That's a pretty good picture of what I get .....
sorry about the gob of stuff . . As I said we are baffled and would really like to find out what is causing this so as to feel more confident that we are writing better code in the future.
Thanks, Howard...
Last edited by admin : 26-Jun-2007 at 21:32. Reason: Use [code] tags for output examples
  #2  
Old 26-Jun-2007, 23:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linked list: differing results on same code.


Quote:
Originally Posted by Howard_L
As I said we are baffled and would really like to find out what is causing this so as to feel more confident that we are writing better code in the

You didn't show output for the other guy's program on the other computer. What did it look like?

You didn't actually tell us what the problem is. I see a problem with the sort routine, and since you printed out everything, I think you can probably see what causes it. Maybe the question should be, "Why would it appear to sort the list correctly on the other machine even though there is a bug?" If so, then my answer is: have you looked at the output on the other machine (with everything printed out the way you showed here)?

When programs act differently on different systems it is often due to different things that the compilers/loaders/linkers do with uninitialized variables.

For example, if your sort routine has a bug that somehow uses the value member of a struct that hasn't been initialized, sometimes it might move up in the list, depending on whether running the code resulted in a value smaller than the numbers you entered into your list or larger.

Some compilers are "neat freaks" and create code that puts zero into uninitialized variables, even though the language standards for C and C++ say that the values are undefined. Others just leave seemingly "random" garbage---which may or not be "random" at all, but often results in very large integer values for example.


Try changing the insert function to something like:

CPP / C++ / C Code:
void insert_element(int input, Linked_List * head)
{
    Linked_List *temp;
    while (head->next_item != NULL) {
        head = head->next_item;
    }
    temp = malloc(sizeof(Linked_List));

    head->next_item = temp;
    head->value = input;
    temp->next_item = NULL;
#if 0
    temp->value    = 666; /* the mark of the beast! */
#else
    temp->value    = 0;
#endif
}

I'll insert a couple of small numbers and see what happens with the sort:

Code:
a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 1 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 2 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. a item: 1, head= 0x804a018, value= 1, next_item= 0x804a028 item: 2, head= 0x804a028, value= 2, next_item= 0x804a038 and the NULL pointer: item: 2, head= 0x804a038, value= 0, next_item= (nil) a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d item: 1, head= 0x804a018, value= 1, next_item= 0x804a038 item: 2, head= 0x804a038, value= 0, next_item= 0x804a028 and the NULL pointer: item: 2, head= 0x804a028, value= 2, next_item= (nil)
Now, this points out that there is a bug in the sort routine. (And it looks like the results on your machine.)

Now, change #if 0 to #if 1
Code:
a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 1 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. b Enter Number: 2 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. a item: 1, head= 0x804a018, value= 1, next_item= 0x804a028 item: 2, head= 0x804a028, value= 2, next_item= 0x804a038 and the NULL pointer: item: 2, head= 0x804a038, value= 666, next_item= (nil) a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d item: 1, head= 0x804a018, value= 1, next_item= 0x804a028 item: 2, head= 0x804a028, value= 2, next_item= 0x804a038 and the NULL pointer: item: 2, head= 0x804a038, value= 666, next_item= (nil)

Remember that in the (bugous) code that you posted, the last value wasn't defined, so the results might be different on different machines. (This shouldn't have been used in comparisons for sorting, anyhow, right? That's the bug.)

Now, have the other guy make the same changes in the program on the other system and see what happens.


Regards,

Dave

Footnote: the bug is not that you didn't initialize the value. The bug is that the sort routine erroneously used it in a comparison that brought it into play. The last item in the list is a dummy (a wasted node whose purpose is only to hold the NULL pointer to indicate the end of the list). The value in that item is irrelevant, but the program, somehow used it.

This is an example that shows you can't "prove" a program is correct by testing. Of course you should test your program as thoroughly as you can, but it may just "happen" that some undefined behavior in the program gives apparently good results for your test cases, but in fact a bug exists, and the bug will manifest itself under different conditions.
  #3  
Old 27-Jun-2007, 00:50
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

Re: Linked list: differing results on same code.


As a side note I would like to comment that I don't see a reason for using a "dummy" node as the last node. I mean why waste resources ? Also the first node, the one created by:
CPP / C++ / C Code:
int main(void)
{
  int menu_return;
  Linked_List *head;                       /* head pointer to list */
  head = malloc(sizeof(Linked_List));      /* malloc list          */
is wasted because the "head->value" is never used for anything.

Naturally making use of "head->value" means that the function declarations have to be changed something like:
CPP / C++ / C Code:
void insert_element(int input, Linked_List ** head);
notice the pointer to pointer which will allow you to manipulate the first node pointer in the function.. ok this was just my opinion and there certainly is many number of ways doing it ... I am just a believer of "neat" code.
  #4  
Old 27-Jun-2007, 06:03
shalombi shalombi is offline
Junior Member
 
Join Date: Feb 2007
Posts: 47
shalombi is on a distinguished road

Re: Linked list: differing results on same code.


Yes after reading all this i realized my first use of a dummy struct was useless and could be replaced simply by a pointer to the struct.

Here is the new and updated code, although i haven't yet changed the code for the sorting and the deleting the input and output work nice and clean and should solve the problem as soon as i adapt the old functions to it.

CPP / C++ / C Code:

#include <stdio.h>

typedef struct Linked_List{
        int value;
        struct Linked_List *next_item;
        } Linked_List;


int menu(Linked_List **head);

void get_new_number(Linked_List **head);//get a number and call insert element to put in list, is passed address of head

void insert_element(int input, Linked_List **head); //get an input and head of the list insert element at next free location by allocation

void print_list(Linked_List *head); //print whole list

int main(void)
{
  int menu_return;
  Linked_List *head;  //head pointer to list
  
  head = NULL;
    
  printf("Welcome, here you can play around with a linked list, please insert as many numbers as you want: \n");
  
  while(1){
  menu_return = menu(&head);
  
  if(menu_return == 1) return 0;
  
  system("cls");
}

  system("PAUSE");	
  return 0;
}

int menu(Linked_List **head){
     char menu_choice;
     int return_value;
     printf(" a - Print the list. \n b - Insert new element. \n c - Delete element by value. \n d - Sort list in ascending order. \n q - To quit.\n");
     menu_choice = getchar();
     switch (menu_choice){
            case 'a':
                 print_list(*head);
                 system("PAUSE");	
                 break;
            case 'b':
                 get_new_number(head);
                 break;
                 
            case 'c':
                 return_value = find_by_value((*head)->next_item, *head);
                 if(return_value == 1) printf("Element destroyed.\n");
                 else printf("Value not found.\n");
                 system("PAUSE");
                 break;
            case 'd':
                 bubble_sort(head);
                 break;
            case 'q':
                 printf("bye. \n");
                 system("PAUSE");
                 return 1;
                 break;   
            default:
                    printf("Invalid input.  \n");
                    system("PAUSE");	
                    break;
                    }
     getchar();
     }



void get_new_number(Linked_List **head){
     int input;
     char buff[10];
     if(scanf("%d",&input)){
          insert_element(input, head);
          }
          else{
               gets(buff);
               }
          
     }



void insert_element(int input, Linked_List **head){
     
     if((*head) == NULL){  //in the case the list has not been initialized
                (*head) = malloc(sizeof(Linked_List));
                (*head)->next_item = NULL;
                (*head)->value = input;
                return;
                }
     
     Linked_List *temp; //temp pointer used to find last item in list
     
     temp = (*head);
     
     while(temp->next_item != NULL){ //while there is another item next keep going through list
                           temp =  temp->next_item;
                           }
     
     temp->next_item  = malloc(sizeof(Linked_List));  //create new entry
     temp->next_item->value = input; //last entry gets the value next pointer value is uninitialised
     temp->next_item->next_item = NULL;  //last item pointer set to null to avoid bad dereference
     
     }


void print_list(Linked_List *head){
     
     int i;
     
     for(i = 0 ; head != NULL ; i++, head = head->next_item){ //while there is another item next keep going through list
                            printf("%d items value = %d \n",i, head->value);
                           }
      
     }
  #5  
Old 27-Jun-2007, 09:20
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 469
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: Linked list: differing results on same code.


Good , so yer not upset at yet another monster from moi. ? . ?
So it turns into an Interesting discussion after all. I'm wondering now :
Quote:
Originally Posted by davew7x
Some compilers are "neat freaks" and create code that puts zero into uninitialized variables
...since we both ran the same .exe with the different results,
would that mean the OS is 'filling in the blanks' ?
Would that vary according to certain ifndef sorts of specifications?
(I don't know much about that yet)
Quote:
Originally Posted by davew7x
You didn't show output for the other guy's program
Right... should have gotten a look at that. I have since been told:
Quote:
Originally Posted by shalombi
yes my uninitialized variable was in the 60 000 so thats our problem.
Quote:
Originally Posted by davew7x
You didn't actually tell us what the problem is
Well, I hadn't looked at it but a couple of hours, what do you want
Actually, I had a few ?marks about the head declaration and moving head->next_item->next_item->next_item was really causing a brain lockup on my end.
I would think pointing a couple of local pointers might be more clear, but really what do I know? So I guess I'm saying I saw a lot of things that looked like they may cause the problem, but my attention was drawn more to the head declaration not being a pointer. The sort blew me away....

I'm glad to see Shalombi's right on a re-write. It will be interesting see what he does differently.
Like seprich said, there certainly is many number of ways doing it! (skin a cat)
Thanks guys, Howard;
  #6  
Old 27-Jun-2007, 09:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linked list: differing results on same code.


Quote:
Originally Posted by Howard_L
...since we both ran the same .exe with the different results,
would that mean the OS is 'filling in the blanks' ?
I hadn't thought about it until I saw you mention it in your first post. Apparently this is the case. At load time, apparently the operating system can fill in uninitialized stuff any way that it darn well pleases. Sometimes this masks problem areas in the code itself and makes one think that it's OK when it really has bugs.


When you get the "minor" problem of running through the list without going too far, you might notice that, generally speaking, you can't perform a bubble sort with one trip (or maybe you noticed it already).

If I clean things up a little, and only print the values, the existing bubble sort makes it look like the following.
I have entered values 4, 3, 2, 1 into the list (these operations not shown here).

Then I print the list and apply the bubble sort function. It needs several passes to get it sorted.

Code:
a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. a value= 4 value= 3 value= 2 value= 1 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d value= 3 value= 2 value= 1 value= 4 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d value= 2 value= 1 value= 3 value= 4 a- Print , b- Insert , c- Delete by value , d- Sort ascending , q- quit. d value= 1 value= 2 value= 3 value= 4

Go back and look at bubble sorts that you did with arrays. Didn't you always have nested loops?

By the way, I hate the menu choices. After all, why not use something like
'i' for insert,
'p' for print,
's' for sort,
'd' for delete,
etc.

It's never too early to consider human engineering.

Regards,

Dave

Footnote: back in the days when the microprocessor world was young (and although I wasn't actually young, I was younger), I worked on a system that had two text editors (line editors actually). These were run from a "dumb terminal" so there was no on-screen menu. No drop-down menu, no right-click context menu (no mouse to click, anyhow), no nothing. All commands were entered from the keyboard.

One editor had a command 'k' for "Keep", meaning "keep everything in the current text buffer by storing the current text buffer in a file."

The other editor had a command 'k' for "Kill", meaning "erase everything in the current text buffer and start all over again."

Imagine how much fun it was erasing all of the work of your editing session by accidentally telling it to "kill" it when you meant to "keep" it.
Last edited by davekw7x : 27-Jun-2007 at 11:20.
  #7  
Old 27-Jun-2007, 10:01
shalombi shalombi is offline
Junior Member
 
Join Date: Feb 2007
Posts: 47
shalombi is on a distinguished road

Re: Linked list: differing results on same code.


I know very well how to implement a bubble sort, but since this was about linked lists i saw no need of putting the extra loop, the swap function is what i wanted to look at.

The new linked list is much simpler, i will post the new one soon, it will make for a nice example.
  #8  
Old 27-Jun-2007, 11:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linked list: differing results on same code.


Quote:
Originally Posted by shalombi
I know very well how to implement a bubble sort...


Actually, I was responding to Howard's post (notice the "Quote:Originally Posted by howard_L") to mention that, above and beyond the problem of going too far in the sort function, it didn't really sort the list. He didn't actually tell me whether that was the error he referred to in the first post of this thread or it was the creeping zero from the last node. (And I did point out the fact that he never actually articulated the error that was so puzzling.)

I didn't mean to talk down to anyone; I just know (from personal experience) that I can get so involved in correcting a technical detail that I sometimes forget what the whole point was. Result is a clean, elegant, beautifully implemented program that doesn't meet the Program Specification.

Regards,

Dave
  #9  
Old 27-Jun-2007, 13:02
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 469
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: Linked list: differing results on same code.


Ok... guess I should have done this from the git-go.
I thought the 'picture' (output) showed the problem better than I could explain
and I felt like the post was imposing enough as it was and it was late.

My Original Question (/error/problem/anomaly) , by Howard_L:
I originally ran the program the way I recieved it:
It did NOT print the additional line showing what was in head after loop finished.
CPP / C++ / C Code:
void print_list(Linked_List *head)
{
  int i = 0;
       /* while there is another item next keep going through list */
  while(head->next_item != NULL){
    i++;
    printf("%d items value = %d \n",i, head->value);
    head = head->next_item;
  }
}
- The first print would show the data as I entered.
- After one sort the line with the largest value in the list would disappear and
. . a line with a 'value' of '0' would appear on the last line.
- Further sorts would work correctly, moving the '0' to the top...
- ... OK - That was my first head scratcher....

So I placed the extra printf()'s in print() to try to find out what was going on:
CPP / C++ / C Code:
void print_list(Linked_List *head)
{
  int i = 0;
  while(head->next_item != NULL){
    i++;
    /* printf("%d items value = %d \n",i, head->value);  replace for tesing with: */
    printf("      item: %d, head= %p, value= %2d, next_item= %p \n", i, (void*)head, head->value, (void*)head->next_item );
    head = head->next_item;
  }
  printf("and the NULL pointer: \n      item: %d, head= %p, value= %2d, next_item= %p \n\n", i, (void*)head, head->value, (void*)head->next_item );
}
- Then I could see what was happening.
. . The largest value would go to the head.
. . This is where I started to get bogged in confusion about whether head 'was' and
. . Whether head 'should be' holding a struct or just a pointer and, and, and, (I still am).
- Then I noticed how adding another item and then sorting would actually relace
. . the last 'add' with a '0'
- ok, so I guess I did think the sort might be a problem, but would it be different if head
. . were a pointer rather than a struct... I didn't know....

So that provided some things to think about.
I told Shalombi what I was getting.
He told me he was NOT getting that and voila! The second head scratcher!

Now, If we both got the same problem I guess we would have addressed that,
but getting different results EVEN with the same .exe????
I asked if it would be ok with him to call in the big guns... and so ... here I'm is.
Confusing everyone with my descriptions. (or lack thereof)
Does that describe it (or not) well (or not) enough?
Hope that helps (really), sorry to all I have harmed , Howard;
  #10  
Old 27-Jun-2007, 13:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linked list: differing results on same code.


Quote:
Originally Posted by Howard_L

I thought the 'picture' (output) showed the problem better than I could explain
But there were at least two things "wrong" with the picture: The superfluous zero value that popped up on your printout, and the fact that there was a function with the name "sort" in it that didn't actually sort.

Furthermore, perhaps other people would have liked to help (and could have helped with fewer words and less soap-boxing than me), but couldn't be bothered to figure out what the program was supposed to do in the first place---and what it was not doing to your satisfaction---while wading through the output that you showed and referencing it back to the program. (And then looking for tell-tale uninitialized variables that could be causing inconsistent behavior.)

Quote:
Originally Posted by Howard_L
I originally ran the program the way I recieved it:
Are you talking about running the .exe that was sent to you or running an .exe that you compiled from source that was sent to you? Or both? Or what?
Quote:
Originally Posted by Howard_L
It did NOT print the additional line showing what was in head after loop finished.
.
.
.


I told Shalombi what I was getting.
He told me he was NOT getting that
.
.
.

but getting different results EVEN with the same .exe????
.
.
.
When you changed the program at your end, you were no longer running the same .exe. Did you send your .exe or did the other guy or did the other guy recompile with your changes and send you the new .exe? Or were you just describing things to each other without actually comparing, line for line and character by character, the differing outputs.

Quote:
Originally Posted by Howard_L
sorry to all I have harmed

You forgot to put the smiley, since you are obviously kidding. (On the other hand, I rarely if ever put smileys anywhere. You can always tell when I am kidding by noticing the twinkle in my eye and by the tone of my voice.)

I, for one, do not regret the thread, since I learned something (about the same .exe giving different results on different versions of operating system, apparently due to the way uninitialized variables are loaded).

That makes it a good day for me. (Not because I deliberately use uninitialized variables in my programs, but because I spend some fair amount of time explaining to people why their programs are "wrong" even though the programs seem to be giving the "right" answer.)

A little syllogism appropriate to the occasion:

Any day that I learn something is a good day.
I learn something every day that I come here.
Therefore every day that I come here is a good day.

Regards,

Dave

"We can face our problem.
We can arrange such facts as we have
with order and method."

Hercule Poirot
--- in Murder on the Orient Express
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Help in C Print is not working with LinkList batman3280 C Programming Language 3 09-Mar-2006 19:03
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 14:13
search linked list itsmecathys C++ Forum 20 18-Apr-2005 01:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.