Pointer to structure

Discussion in 'C++' started by Tomek, May 2, 2005.

  1. Tomek

    Tomek Guest

    Hello,

    lets say i have some structure

    struct Node {
    int key ;
    Node *left, *right ;
    }

    then in main() i declare pointer to structure like this

    Node *root = NULL ;

    then i want to send this pointer to function which will
    add new Node, definition of this function looks like this:

    void AddNode(Node *root) {
    Node *anothernode = new Node ;
    anothernode->left = anothernode->right = NULL ;

    if( root == NULL )
    root = anothernode ;
    // ok rest code is not important here
    }

    the main problem is when i return from function AddNode
    my pointer called "root" doesnt remember address of pointer
    "anothernode", any idea why ?

    Thanks in advance,
    Regards
    tommy
     
    Tomek, May 2, 2005
    #1
    1. Advertising

  2. Tomek

    Rade Guest

    > void AddNode(Node *root) {
    > Node *anothernode = new Node ;
    > anothernode->left = anothernode->right = NULL ;
    >
    > if( root == NULL )
    > root = anothernode ;
    > // ok rest code is not important here
    > }
    >
    > the main problem is when i return from function AddNode
    > my pointer called "root" doesnt remember address of pointer
    > "anothernode", any idea why ?


    Because the argument 'root' of AddNode is not the same object as the global
    variable 'root'. The former exists only within the AddNode function, but
    hides the latter because they have the same names (imagine you have renamed
    each occurence of 'root' within AddNode - the code would work the same
    way!).

    Fix it in one of two ways:

    1) If you know that AddNode should specifically modify the global variable
    root, then you don't need to pass it as an argument. Simply write:

    void AddNode()
    {
    Node *anothernode = new Node ;
    anothernode->left = anothernode->right = NULL ;

    if( root == NULL )
    root = anothernode ;
    // ok rest code is not important here
    }

    2) If AddNode has to modify not only the global variable root, but also any
    other Node * variable, then pass it as a reference to a Node pointer:

    void AddNode(Node * &root)
    {
    Node *anothernode = new Node ;
    anothernode->left = anothernode->right = NULL ;

    if( root == NULL )
    root = anothernode ;
    // ok rest code is not important here
    }

    3) An old-fashioned, more-like-C solution, which is probably equivalent to
    (2) with most compilers:

    void AddNode(Node **root)
    {
    Node *anothernode = new Node ;
    anothernode->left = anothernode->right = NULL ;

    if( *root == NULL )
    *root = anothernode ;
    // ok rest code is not important here
    }

    Rade
     
    Rade, May 2, 2005
    #2
    1. Advertising

  3. Tomek

    Rade Guest

    Note also that, when you make it work, if root is not NULL, you have a
    memory leak (anothernode allocated and never freed). Do you really need to
    allocate it before you check if root is NULL?

    Rade
     
    Rade, May 2, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    852
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    6
    Views:
    377
    Jack Klein
    Aug 27, 2006
  4. Replies:
    4
    Views:
    1,288
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    27
    Views:
    1,648
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page