#31
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    Well, knowing that the function would make changes to the queue, it just looked odd the way that it was.

    Lunch is over and I have to get back to work now, plus I won't get home until late tonight.
    No problem, I understand. Do you imagine you could possibly help me figure out how to make it work?
  2. #32
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by peripatein
    No problem, I understand. Do you imagine you could possibly help me figure out how to make it work?
    Either you, or any other person here. I'd truly appreciate any concrete advice.
  4. #33
  5. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    I will reiterate my previous advice: learn how to use your debugger to step through the code and see what's happening on your own, instead of using forums as a debug-by-proxy service. It's in your own best interests.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  6. #34
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by ptr2void
    I will reiterate my previous advice: learn how to use your debugger to step through the code and see what's happening on your own, instead of using forums as a debug-by-proxy service. It's in your own best interests.
    I appreciate your advice, and normally I would use the debugger, except that here it is not to much avail. I know, roughly, where the program crashes, but I am not sure what's actually causing that and also how to fix it.
    Why couldn't you help me figure it out? I am not lazy at all! I wrote most of this code myself and have dedicated days and hours to try to remove these bugs. I could truly use your help.
  8. #35
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Maybe I've just been spoiled by the MS Visual Studio debugger. In order to investigate a crash of our application, which is rather large, I run it in the debugger until it crashes. Then I look at the call stack to see the last function that I had written which was executed (often the actual crash will be in a library somewhere). I select that call stack item to go back to that call and I examine the variables. Most often, it's an object that didn't get instantiated. If it doesn't make sense to me why that would be, I look at the code in that function that should have set everything up. That will give me an idea where to put a breakpoint. So I set the breakpoint(s) and restart it. Then when it reaches the breakpoint, I can see what values have gotten passed in and I can then single-step through the code while examining the variables.

    It is by finding where the program crashes and examining what happens as we approach that crash that enables me to figure out what the problem is and how to fix it.

    BTW, you're using a lot of recursion there. Stepping that through the debugger may be worthwhile to verify that it's doing what you think.
  10. #36
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    Maybe I've just been spoiled by the MS Visual Studio debugger. In order to investigate a crash of our application, which is rather large, I run it in the debugger until it crashes. Then I look at the call stack to see the last function that I had written which was executed (often the actual crash will be in a library somewhere). I select that call stack item to go back to that call and I examine the variables. Most often, it's an object that didn't get instantiated. If it doesn't make sense to me why that would be, I look at the code in that function that should have set everything up. That will give me an idea where to put a breakpoint. So I set the breakpoint(s) and restart it. Then when it reaches the breakpoint, I can see what values have gotten passed in and I can then single-step through the code while examining the variables.

    It is by finding where the program crashes and examining what happens as we approach that crash that enables me to figure out what the problem is and how to fix it.

    BTW, you're using a lot of recursion there. Stepping that through the debugger may be worthwhile to verify that it's doing what you think.
    I happen to know that the recursions work well. What I am not too familiar with are the initialization and the usage of malloc's, pointers. These are rather delicate things which I simply do not fathom sufficiently at this point to enable me to actually fix the problem myself. Through your assistance I was hoping I could considerably enhance my understanding. The process of learning from personal mistakes is absolutely valuable, and I feel I could not get there on my own, without your assistance. I have tried using the debugger as you suggested. I always set breakpoints and watches etc., however, as these topics are still somewhat tricky to me, I fear I would be left but to hope that you will agree to help me rid my program of its bugs, thus enabling me to master these challenging topics in C.
  12. #37
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by peripatein
    I happen to know that the recursions work well. What I am not too familiar with are the initialization and the usage of malloc's, pointers. These are rather delicate things which I simply do not fathom sufficiently at this point to enable me to actually fix the problem myself. Through your assistance I was hoping I could considerably enhance my understanding. The process of learning from personal mistakes is absolutely valuable, and I feel I could not get there on my own, without your assistance. I have tried using the debugger as you suggested. I always set breakpoints and watches etc., however, as these topics are still somewhat tricky to me, I fear I would be left but to hope that you will agree to help me rid my program of its bugs, thus enabling me to master these challenging topics in C.
    OK, so having run the debugger again, the program crashed at the second line of the following function;
    Code:
    int isNotSpecificOrder(Order ** iter){
    	Order * tmp = *iter;
    	while (tmp->isSpecific && tmp != NULL)
    		tmp = tmp->next;
    	if (tmp == NULL)
    		return 0;
    	return (tmp->orderID);
    }
    
    The function was called by:
    
    int addDriver(TaxiSystem * system, char * name, int priority){
        int order;
        int spec;
        if (doesDriverExist(system->driversQueue, name))
            return 1;
        order = findOrderByName(system->ordersQueue, name);
        if (order){
            EraseOrder(&system->ordersQueue, order);
            return 1;
        }
    	spec = isNotSpecificOrder(&system->ordersQueue);
        if (system->driversQueue == NULL && spec != 0) 
    		EraseOrder(&system->ordersQueue, spec);
    	return(createDriverAndAddByPriority(system, name, priority));	
    }
    Now, would you please help me figure out why this is happening?
  14. #38
  15. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Here's a hint: What happens when you dereference a NULL pointer?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  16. #39
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by ptr2void
    Here's a hint: What happens when you dereference a NULL pointer?
    I have fixed that one. The program does not crash any longer. However, it does not update the queues accordingly and as needed. Would you be so kind as to please help with that by examining the relevant function?
  18. #40
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    My method for such problems, both in the design phase and debugging, is to play computer with pencil and paper. Go through the code step-by-step and write down the values of all the variables, changing them as the program changes them, kind of like you would do with a debugger. But for array indexing and linked-structure problems (yours is the latter type), this method goes beyond the debugger because you can sketch the data structures as you create and change them. Just remember that the essential requirement for this method to work is that you only do what the code tells you to do, step-by-step.

    The only way that we could solve this problem for you would be for us to employ that method. The problem is that it can be time-consuming, especially for us who can only steal away a few minutes at a time from our own work. You would be the much better candidate to do this work, especially since you understand far better than we just what you want the code to do.

    A variation of this technique is to conscript a volunteer to sit down and let you explain in step-by-step detail what your program does. The error will become obvious to you before you're even half-way through that evolution.

    A primary debugging goal of these techniques is to get us to see what our code is really doing in contrast to what we "know" that it's doing. Our brains have a bad habit of filling in blanks, so that when we look at something we have written we only see what we think we have written, not what we have actually written. You need to force the brain to do something different with what you've written in order to actually read it -- eg, when proof-reading prose that I've written, I will read it out aloud to myself in order to see where I had left out words (I think faster than I can touch-type).

    You should try to technique of sketching out the queues as they are created. Be sure to keep track of every single pointer value.
  20. #41
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    I feel quite lost. I have been sitting here for hours, debugging it, writing it down as well, step-by-step. And yet I am still unable to figure out why the following function would not initialize my Drivers' struct.
    Any suggestions?
    Code:
    int createDriverAndAddByPriority(TaxiSystem* system, char* name, int priority){
      Driver *new_node = NULL, *cur = NULL, *prv = NULL;
      new_node = (Driver*) malloc(sizeof(Driver));
      if (new_node == NULL){
       printf("Fatal error: memory allocation failed!\n");
       return 0;
      }
      strcpy(new_node->name, name);
      new_node->priority = priority;
      cur = system->driversQueue;
      if (!cur)
       new_node->next = cur;
      else{
       while (cur && (cur->priority < new_node->priority)){
        prv = cur;
        cur = cur->next; 
       }
       new_node->next = cur;
       prv->next = new_node;
      }
      return 1;
     }
  22. #42
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Where do you set system->driversQueue to point to the new node?
  24. #43
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    Where do you set system->driversQueue to point to the new node?
    Okay, I have made some headway and am quite happy about that :-).
    Problem is, my program seems to override the drivers or not update the queue correctly.
    Here is the code:
    Code:
    int createDriverAndAddByPriority(TaxiSystem * ptr, char* name, int priority){
    	Driver * new_node = NULL, *cur = NULL, *prv = NULL;
    	new_node = (Driver*) malloc(sizeof(Driver));
    	if (new_node == NULL){
    		printf("Fatal error: memory allocation failed!\n");
    		return 0;
    	}
    	strcpy(new_node->name, name);
    	new_node->priority = priority;
    	cur = ptr->driversQueue;
    	if (!ptr->driversQueue){
    		ptr->driversQueue = new_node;
    		new_node->next = NULL;
    	}
    	else{
        	while (cur && (cur->priority < new_node->priority)){
            	prv = cur;
    			cur = cur->next;	
    		}
        	if (!prv){
            	ptr->driversQueue = new_node;
    			new_node->next = prv;
    		}
        	else{
         	   prv->next = new_node;
    			new_node->next = cur;
    		}
    	}
    	return 1;
    }
  26. #44
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Originally Posted by peripatein
    Problem is, my program seems to override the drivers or not update the queue correctly.
    What does that mean?

    Obviously, you expect it to do one thing and it's doing something else. But you still think that we can read minds. We cannot read your mind! You have to tell us what's happening!

    Provide a specific example. You make some entries in a particular order. You expect certain specific output. You get output which is different.

    OK:
    1. What was your input?
    2. What specifically do you expect as output?
    3. What specifically do you get instead?
    4. What's the code that produces that output? (on the off chance that the queues are correct, but the print routine is messed up)
  28. #45
  29. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    I only have a few minutes right now.

    This looks strange -- BTW, I just took the open braces out of hiding to make it more readable for me; K&R depends absolutely on proper indenting and to your credit you do take care of that, so "bravo" on that point:
    Code:
            while (cur && (cur->priority < new_node->priority))
            {
                prv = cur;
                cur = cur->next;    
            }
            // I assume that !prv (AKA "prv == NULL") is true if the while loop never runs
            //  I further assume that means that the new node needs to be placed first
            //      in front of all other nodes already there.
            if (!prv)  
            {
                ptr->driversQueue = new_node;  // based on assumptions above, this looks correct
                new_node->next = prv;   // but since prv is NULL, this drops all pre-existing
                                    //  nodes, so that if you had 4 nodes before adding this
                                   //   one, now you only have one.
                                    // I'm guessing you should assign cur instead of prv
            }
            else   // prv != NULL, so this appears alright
            {
               prv->next = new_node;
                new_node->next = cur;
            }
    Last edited by dwise1_aol; June 14th, 2013 at 05:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo