+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    分かりますか。 carl33p's Avatar
    Join Date
    Feb 2006
    Location
    Gville, FL
    Posts
    7,170
    Blog Entries
    9

    C++ Array of Pointers

     
    Hey guys,

    Ive made this small test program. Why do all the pointers in the array end up pointing to the last object I insert in?

    Code:
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    
    /************************************************/
    
    class TreeNode {
    	public:
    	  string item;      // The printable token
             TreeNode *left;    // Pointer to left subtree.
             TreeNode *right;   // Pointer to right subtree.
    };
    
    /************************************************/
    
    void BT(string tok);
    
    TreeNode ** Arr = new TreeNode*[100];
    int a = 0;
    
    /************************************************/
    
    int main(int argc, char** argv) {
    
    		BT( "a" );
    		BT( "b" );
    		BT( "c" );
    
    		cout << Arr[0]->item << endl;	
    		cout << Arr[1]->item << endl;	
    		cout << Arr[2]->item << endl;
    		
    	return 0;
    }
    
    /************************************************/
    
    void BT(string tok){
    	TreeNode tr;
    	tr.item = tok;
    
           TreeNode *root=&tr;
    
    
    	Arr[a++] = root;
    
    	cout << Arr[a-1]->item << endl;
    }
    Heres the output I get:
    Code:
    a
    b
    c
    c
    c
    c
    Last edited by carl33p; April 25th, 2009 at 04:06 PM.

  2. #2
    分かりますか。 carl33p's Avatar
    Join Date
    Feb 2006
    Location
    Gville, FL
    Posts
    7,170
    Blog Entries
    9
    I found a work around for creating an array of pointers to TreeNodes.

    But Im still not sure about this test program.

  3. #3
    Super F@D Folder
    Join Date
    Jun 2004
    Posts
    5,091
    When you create an object, it allocates the memory for the object, then it calls the constructor.

    Because tr is a local variable it goes out of scope at the end of the method, and as such is automatically deleted. Once that happens, the memory is marked as free, and the next time the method is called an object is created in the exact same location in memory. To see what I mean try the code below.

    Code:
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    
    /************************************************/
    
    class TreeNode {
    	public:
    	  string item;      // The printable token
             TreeNode *left;    // Pointer to left subtree.
             TreeNode *right;   // Pointer to right subtree.
    };
    
    /************************************************/
    
    void BT(string tok);
    
    TreeNode ** Arr = new TreeNode*[100];
    int a = 0;
    
    /************************************************/
    
    int main(int argc, char** argv) {
    
    		BT( "a" );
    		BT( "b" );
    		BT( "c" );
    
    		cout << Arr[0]->item << endl;	
    		cout << Arr[1]->item << endl;	
    		cout << Arr[2]->item << endl;
    		
    	return 0;
    }
    
    /************************************************/
    
    void BT(string tok){
    	TreeNode tr;
    	tr.item = tok;
    
           TreeNode *root=&tr;
    
    
    	Arr[a++] = root;
    
    	//print out the memory address & item
    	cout << Arr[a-1] << ": " << Arr[a-1]->item << endl;
    }
    that prints out the address of the object & value of item when it's saved. you should see every time the function is called the object is created at the exact same location. To fix that you need to use the new keyword when you create the object. It's kind of like using malloc in C

    /edit - here's what i got when I ran my code...
    Code:
    0xbfccf9d0: a
    0xbfccf9d0: b
    0xbfccf9d0: c
    As you can see, each object is at the same address as I said. new keyword will fix it!

    -Kevin
    Last edited by sr71000; April 25th, 2009 at 07:42 PM.

  4. #4
    分かりますか。 carl33p's Avatar
    Join Date
    Feb 2006
    Location
    Gville, FL
    Posts
    7,170
    Blog Entries
    9
    I see. That explains a lot. Thanks.

    Been using Java too much...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C and pointers help
    By lost-and-found in forum Webmastering and Programming
    Replies: 1
    Last Post: March 15th, 2009, 10:30 AM
  2. c++ pointers
    By lost-and-found in forum Webmastering and Programming
    Replies: 5
    Last Post: August 23rd, 2004, 08:21 PM
  3. Pointers?
    By Brainchild in forum IMO Community
    Replies: 8
    Last Post: July 23rd, 2004, 06:58 PM
  4. Pointers as parameters...HELP!
    By squeech in forum Webmastering and Programming
    Replies: 1
    Last Post: September 16th, 2003, 10:45 PM
  5. C++ pointers
    By Damien019 in forum Webmastering and Programming
    Replies: 5
    Last Post: May 1st, 2003, 11:00 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews