C++ pointer issue

This is a discussion on C++ pointer issue within the C++ Programming forums, part of the General Programming Boards category; I am using a binary tree whereby the search item is return using pass by reference i.e. "searchElement" store the ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18

    C++ pointer issue

    I am using a binary tree whereby the search item is return using pass by reference i.e. "searchElement" store the result

    Code:
    int BSTree::retrieve ( String searchKey, Car *searchElement ) 
    {
    	return ( retrieveSub (root, searchKey, searchElement));
    }
    
    //recursion partner fucntion of retrieve
    int BSTree::retrieveSub ( BSTreeNode *ptr, String key, Car *element ) 
    {	
    	if ( ptr == 0 )
    		return 0;
    	else if ( key < ptr->element.key() )
    		return retrieveSub (ptr->left, key, element);
    	else if ( key > ptr->element.key() )
    		return retrieveSub (ptr->right, key, element);
    	else
    	{
    		//at this point, the item address is correctly assign to element
                    //however, when return to the retrieve function, the searchElement is NULL
                    element = &ptr->element;
    		return 1;
    	}
    }
    During debugging, the watch value of element is correct, but when return to parent function, the searchElement is NULL. (both element and searchElement should point to the same address.) What am I missing here? Thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    element is a local variable of retrieveSub. modifying it is useless, it won't affect anything else

    maybe you wanted to modify what it pointed to? or something?

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    The retrieveSub method will make a copy of the element pointer which has the value of the address pointed by the pointer. You can change the values of attributes inside the object but you can't change the this value of element. CMIIW, I'm not too sure about this.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you can pass the searchElement pointer by referense, then it will be changed also outside the retrieveSub function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Thanks for the reply. Maybe my concept was wrong, I tot passing a pointer to the retrieveSub can do the trick. vart, here's my 2nd attempt to pass the pointer by reference, correct me if i m wrong, thanks.

    Code:
    int BSTree::retrieve ( String searchKey, Car &searchElement ) 
    {
    	return ( retrieveSub (root, searchKey, searchElement));
    }
    
    //recursion partner fucntion of retrieve
    int BSTree::retrieveSub ( BSTreeNode *ptr, String key, Car &element ) 
    {	
    	if ( ptr == 0 )
    		return 0;
    	else if ( key < ptr->element.key() )
    		return retrieveSub (ptr->left, key, element);
    	else if ( key > ptr->element.key() )
    		return retrieveSub (ptr->right, key, element);
    	else
    	{
                    element = ptr->element;
    		return 1;
    	}
    }
    However, the compiler complains error for this version. Perhaps due to the varying size of the Car object.

    class Car
    {
    private:
    ..
    public:
    Passenger *head, *tail; //a dynamic linked list
    }

    My last resort is to create a global Car* to store the retrieve element.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    global is not a good idea

    what is the compiler error?

    And I remember to say - reference to pointer, not a reference to object...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Sorry for the wrong implementation. I have change to reference to pointer, but it complains the same error during execution:

    doexit(int code = CXX0065: Error: unable to obtain expression value, int quick = 221716, retcaller=1376944)

    where the destructors are called and the application exits.
    Code:
    int BSTree::retrieve ( String searchKey, Car *&searchElement ) 
    {
    	return ( retrieveSub (root, searchKey, searchElement));
    }
    
    //recursion partner fucntion of retrieve
    int BSTree::retrieveSub ( BSTreeNode *ptr, String key, Car *&element ) 
    {	
    	if ( ptr == 0 )
    		return 0;
    	else if ( key < ptr->element.key() )
    		return retrieveSub (ptr->left, key, element);
    	else if ( key > ptr->element.key() )
    		return retrieveSub (ptr->right, key, element);
    	else
    	{
                    *element = ptr->element;
    		return 1;
    	}
    }
    Thanks for ur advice

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    This
    *element = ptr->element;

    will copy the element to the location pointed by the pointer
    This
    element = &ptr->element;
    changes the pointer to point the element stored in the list.
    You should check what you really want; according to your original code I thoght you wanted the second way
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Thanks again vart, I made a silly mistake juz now. You are correct, I want the second way but did it in the wrong way. Now it works brilliantly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  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. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21