Thread: Pointers (temporary usage, not as alias)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52

    Pointers (temporary usage, not as alias)

    I am trying to implement a splay tree. A part of my code is below. I have figured out that this statement makes currentnode an alias for root even though the previous line reserves a memory for currentnode (or at least I think so). What I need is a way to use the currentnode as something independent from root, but with initial value (to point to) its contents.

    Code:
    myNode *currentnode = currentnode->left = currentnode->right currentnode->parent = new myNode;
    currentnode = root;
    while(true){
    	if(a < currentnode->value){ // we examine if it can be a left child
    		if (currentnode->left != NULL){
    			currentnode = currentnode->left;
    		}
    		else{
    			myNode *added = new myNode(a, NULL, NULL, currentnode);
    			currentnode->left = added;
    			cout << "currentnode->left address before rotations: " << currentnode->left << endl;
    			cout << "root->left address before rotations: " << root->left << endl;
    			cout << "currentnode address before rotations: " << currentnode << endl;
    			cout << "root address before rotations: " << root << endl;
    			rotations(added);
    			cout << "currentnode address after rotations: " << currentnode << endl;
    			cout << "root after rotations: " << root << endl;
    			currentnode = NULL;
    			return true;
    		}
    Thanks is advance.
    Last edited by myle; 08-19-2007 at 12:21 PM. Reason: in comments, doesn't change anything

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you want to make a new currentnode (using the "new" keyword), then copy the contents:
    Code:
    *currentnode = *root;
    Bear in mind, however, that this copies ALL of the root node into currentnode, including the pointers to left/right and parent for example. If you don't want that, you'll have to copy the content field by field.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    Now it seems so obvious, but I couldn't think of it. Thanks a lot.

    EDIT: The assign operator uses the copy constructor. So if I haven't define one, then the default behaviour is to copy each element of the object, even the pointers. Does it allocate space for these pointers?

    EDIT2: Is this line necessary:
    Code:
    myNode *currentnode = currentnode->left = currentnode->right = currentnode->parent = new myNode;
    Can I replace it with:
    Code:
    myNode *currentnode;
    *currentnode = *root;
    If yes, then how I can get rid of the warning:

    uninitialized local variable 'currentnode' used
    Last edited by myle; 08-19-2007 at 12:15 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> the default behaviour is to copy each element of the object, even the pointers. Does it allocate space for these pointers?
    No. The default copy constructor will not allocate space. Both the source and the target of the copy will point to the same objects.

    >> how I can get rid of the warning: uninitialized local variable 'currentnode' used
    You have to allocate space for current node. Your original code used new myNode. Do that here before copying the contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lifetime of temporary during exception throw
    By brewbuck in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2009, 04:08 PM
  2. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  3. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  4. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM