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

    Join Date
    Jul 2003
    Posts
    33
    Rep Power
    12

    Debugging: Using infromation from a list in multiple functions


    I am saving the commands in to a list. The data is in the list, I have stepped through the function it is there until return executes, but when cmd_assign() exits it clears the list. Any suggestions on what might be causing this? Or how to track down this type of issue?



    Code:
    int main()
    {
    std::list<Network_Prog> cmds;
    
    cmd_assign(cmds);
    
    
    return 0;
    }

    Code:
    int cmd_assign(std::list<Network_Prog> cmds_);
    
    int cmd_assign(std::list<Network_Prog> cmds_)
        {
    
        std::list<Network_Prog>::reverse_iterator i;
        cmds_.resize(cmds_.size() + 1);
        i = cmds_.rbegin();
    
    
        std::string ping_cmd("\\ping.exe");
        std::string ping_full_cmd(search_file_windir(ping_cmd));
        i->ping(ping_full_cmd);
    
        std::string traceroute_cmd("\\tracert.exe");
        std::string traceroute_full_cmd(search_file_windir(traceroute_cmd));
        i->traceroute(traceroute_full_cmd);
    
        std::string netstat_cmd("\\netstat.exe");
        std::string netstat_full_cmd(search_file_windir(netstat_cmd));
        i->netstat(netstat_full_cmd);
    
        std::string ipcfg_cmd("\\ipconfig.exe");
        std::string ipcfg_full_cmd(search_file_windir(ipcfg_cmd));
        i->ipcfg(ipcfg_full_cmd);
    
    
        return 0;
        }





    Thanks!!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Basic C. Declare cmds_ to be a pointer. Or more simply, since you are using C++, declare it as a reference.

    When you pass a parameter to a function, regardless of the type, you pass the value of that parameter -- AKA "call by value". Then whatever that function may do to the parameter has no effect whatsoever to the variable the value was originally taken from.

    E.g.:
    Code:
    void swap(int a, int b)
    {
        int temp;
    
        temp = a;
        a = b;
        b = temp;
    }
    
    int main(void)
    {
        int x, y;
    
        x = 1;
        y = 2;
        swap(x,y);
        printf("x=%d, y=%d\n",x,y); 
        /* prints out "x=1, y=2" because swap didn't take */
        return 0;
    }
    The values of x and y were not swapped, because the swap function only worked with their values, not with them.

    The C solution, which is also used in C++, is to declare that parameter to be a pointer to that variable type and to pass in the address of the variable; e.g.:
    Code:
    void swap(int *a, int *b)
    {
        int temp;
    
        temp = *a;
        *a = *b;
        *b = temp;
    }
    
    int main(void)
    {
        int x, y;
    
        x = 1;
        y = 2;
        swap(&x,&y);
        printf("x=%d, y=%d\n",x,y); 
        /* prints out "x=2, y=1" because swap DID take */
        return 0;
    }
    Here, you give swap() the addresses of the variables, so whatever it does to their contents do affect the original variables.

    In addition, C++ allows you to use references, which is basically the same thing as pointers only you don't have to work explicitly with pointers. It's like "call by reference" in Pascal. E.g.:
    Code:
    void swap(int &a, int &b)
    {
        int temp;
    
        temp = a;
        a = b;
        b = temp;
    }
    
    int main(void)
    {
        int x, y;
    
        x = 1;
        y = 2;
        swap(x,y);
        printf("x=%d, y=%d\n",x,y); 
        /* prints out "x=2, y=1" because swap DID take */
        return 0;
    }
    The key difference is in the function header for swap, where the "&" declares that parameter to be a reference.
    Last edited by dwise1_aol; July 16th, 2003 at 09:57 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    33
    Rep Power
    12
    Thank you for your help.

    I didn't realize I had left & out of the function definition.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo