Thread: Pointer confusion

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Pointer confusion

    Hey, I'm having some issues with pointers. I've got two linked list classes, one is a list of arguments and one is a list of orders. I'm trying to integrate the two together, but it's not quite working.

    What I want to do is pass a pointer to my function, and end up having my pointer point to the root of the argument list of the specified order. But, it just crashs when I try to get data out of it. It works if I preset the data for a non-pointer, which makes me think it's just not copying correctly. Unfortunantly I've never been to great with pointers -,-.

    Here's what I think the issue is, I'll post the whole code at the bottom;
    Code:
    #include <iostream.h>
    
    int main() {
     AiStack a;
     ArgList arg;
     ArgList *args;
    
     arg.NewArg(18);
     a.NewOrder(10000, arg);
    
     cout << a.GetNextOrder(args) << endl;
     cout << arg.GetNextArg(); //works fine
     cout << args->GetNextArg(); //crashs here
    }
    
    int AiStack::GetNextOrder(ArgList *Args) {
     int a = 0;
     OrderNode *copy;
     OrderNode *xerox;
     copy = root;
     while(a != TravOrders) {
      a++;
      if(root->next != 0) {
       root = root->next;
      } else {
       return 0;
      }
     }
    
     xerox = root;
     root = copy;
     TravOrders++;
     Args = &xerox->Args; //This should make Args point to the adress of xerox->args right?
     return xerox->Order;
    }
    Argument list code:
    Code:
    #include <windows.h>
    
    struct ArgNode {
     int Arg;
     ArgNode *next;
    };
    
    class ArgList {
     protected:
      int TravArgs;
      int Args;
      ArgNode *root;
    
     public:
      ArgList() {Args = 0, TravArgs = 1, root = new ArgNode, root->next = 0;}
    
      void NewArg(int arg);
    
      int GetLastArg();
      int GetNextArg();
      int GetNumberOfArgs() {return Args;}
    };
    
    void ArgList::NewArg(int arg) {
     ArgNode *copy;
     copy = root;
    
     while(root->next != 0)
      root = root->next;
    
     root->next = new ArgNode;
     root = root->next;
     root->Arg = arg;
     root->next = 0;
    
     root = copy;
     Args++; 
    }
    
    int ArgList::GetLastArg() {
     ArgNode *copy;
     ArgNode *xerox;
     copy = root;
    
     while(root->next != 0)
      root = root->next;
    
     xerox = root;
     root = copy;
     return xerox->Arg;
    }
    
    int ArgList::GetNextArg() {
     int a;
     ArgNode *copy;
     ArgNode *xerox;
     copy = root;
    
     while(a != TravArgs) {
      a++;
      if(root->next != 0) {
       root = root->next;
      } else {
       return 0;
      }
     }
    
     xerox = root;
     root = copy;
     return xerox->Arg;
    }
    Order List code:
    Code:
    #include "Arg.h"
    
    struct OrderNode {
     int Order;
     ArgList Args;
     OrderNode *next;
    };
    
    class AiStack {
     protected:
      int TravOrders;
      int Orders;
      OrderNode *root;
    
     public:
      AiStack() {Orders = 0, TravOrders = 1, root = new OrderNode, root->next = 0;}
    
      void NewOrder(int order, ArgList Args);
    
      int GetLastOrder(ArgList *Args);
      int GetNextOrder(ArgList *Args);
      int GetNumberOfOrders() {return Orders;}
    };
    
    void AiStack::NewOrder(int order, ArgList Args) {
     OrderNode *copy;
     copy = root;
    
     while(root->next != 0)
      root = root->next;
    
     root->next = new OrderNode;
     root = root->next;
     root->Order = order;
     root->Args = Args;
     root->next = 0;
    
     root = copy;
     Orders++;
    }
    
    int AiStack::GetLastOrder(ArgList *Args) {
     OrderNode *copy;
     OrderNode *xerox;
     copy = root;
    
     while(root->next != 0)
      root = root->next;
    
     xerox = root;
     root = copy;
     Args = &xerox->Args;
     return xerox->Order;
    }
    
    int AiStack::GetNextOrder(ArgList *Args) {
     int a = 0;
     OrderNode *copy;
     OrderNode *xerox;
     copy = root;
     while(a != TravOrders) {
      a++;
      if(root->next != 0) {
       root = root->next;
      } else {
       return 0;
      }
     }
    
     xerox = root;
     root = copy;
     TravOrders++;
     Args = &xerox->Args;
     return xerox->Order;
    }
    Also, my compiler is spouting this, which doesnt make sence to me. (The value is assigned to a pointer, so it shoulden't care if it's not used in a function, right? And why is it saying it's never used?)
    Code:
    Parameter 'Args' is never used in function AiStack::GetNextOrder(ArgList *)
    'Args' is assigned a value that is never used in function AiStack::GetNextOrder(ArgList *)

    Well thank you for any help, I've been stuck with this problem for a few days now.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    if you don't use Args, why do you have the function take it?
    Code:
    cout << args->GetNextArg(); //crashs here
    args is uninitialized.

    may I suggest more varied variable names?

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Args is suposed to be used, which is possibly the problem.

    args is passed to a.GetNextOrder() which is suposed to pass arg to args. It shouldent be neccesary to initialize something if it's just going to point to something anyway, right?

    And as for the variable names, Args stands for Arguments, and i'm just to lazy to think up more creative names ;P.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Blackroot View Post
    Args is suposed to be used, which is possibly the problem.
    used for what?
    args is passed to a.GetNextOrder() which is suposed to pass arg to args. It shouldent be neccesary to initialize something if it's just going to point to something anyway, right?
    Code:
    int main() {
     AiStack a;
     ArgList arg;
     ArgList *args;
    
     arg.NewArg(18);
     a.NewOrder(10000, arg);
    
     cout << a.GetNextOrder(args) << endl;
     cout << arg.GetNextArg(); //works fine
     cout << args->GetNextArg(); //crashs here
    }
    and what does it point to?
    And as for the variable names, Args stands for Arguments, and i'm just to lazy to think up more creative names ;P.
    what do arg and a stand for then? at least comment what the differences are, please.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    a.NewOrder(10000, arg); //Passes arg to a.Args
     
    cout << a.GetNextOrder(args) << endl; //Paases args so it points to a.Args
    The comments are what it's suposed to be doing.

    As for my variable names, it's kind of my standard. (Confusing & pointless) I wrote it quickly so I could test the class, otherwise it takes me awhile to get the names and such out.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    well, I meant something that would make it less confusing, not more. like, differentiate the simliar variable names.

    your program is crashing because it's dereferencing an uninitialized pointer. Do you know what that means?
    Last edited by robwhit; 09-11-2007 at 01:38 AM.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I agree with the others about your variable names.
    But here is what I think the problem is. In GetNextOrder you assign &xerox->args to the pointer. Problem is that the pointer itself is copied when passed to the function. You can only ever modify the thing it points *to* not *where* it points. For that you will need a **(pointer to a pointer).

    Instead of: cout << args->GetNextArg(); print out args itself and you'll see what I mean. Since you haven't initialised your variables, it'll probably point to some random part of memory. Also print out the value of Args in getNextOrder and you'll see what I mean.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    But here is what I think the problem is. In GetNextOrder you assign &xerox->args to the pointer. Problem is that the pointer itself is copied when passed to the function. You can only ever modify the thing it points *to* not *where* it points. For that you will need a **(pointer to a pointer).
    If I use a **pointer will that correctly assign the value of &xerox->args?

    I understand it's not initialized, but it shouldent need to be if it points to something that is, right?

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Blackroot View Post
    If I use a **pointer will that correctly assign the value of &xerox->args?

    I understand it's not initialized, but it shouldent need to be if it points to something that is, right?
    You don't *need* to initialise any variables, it's just good practice to put them into a common state before you use them.

    And yes, if you use a **pointer then you pass the address of Args to getNextOrder, in the function you assign &(xerox->args) to *Args and Bob's your uncle.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Blackroot View Post
    I understand it's not initialized, but it shouldent need to be if it points to something that is, right?
    but it doesn't point to anything. it has random memory in the pointer, and then you use whatever is at that location.

  11. #11
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Cool, just updated the code and it works with a few kinks. Going to start debugging thoes, but it does work. Thank you Quantum .

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Meanwhile back in the world of C++ we use the Standard C++ Library, and are happy with our perfectly working std::list that we never have to think twice about.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM