Thread: C++ Array of Pointers
-
April 25th, 2009, 04:00 PM #1
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?
Heres the output I get: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; }
Code:a b c c c c
Last edited by carl33p; April 25th, 2009 at 04:06 PM.
-
April 25th, 2009, 05:11 PM #2
I found a work around for creating an array of pointers to TreeNodes.

But Im still not sure about this test program.
-
April 25th, 2009, 07:36 PM #3
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.
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 CCode:#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; }
/edit - here's what i got when I ran my code...
As you can see, each object is at the same address as I said. new keyword will fix it!Code:0xbfccf9d0: a 0xbfccf9d0: b 0xbfccf9d0: c

-KevinLast edited by sr71000; April 25th, 2009 at 07:42 PM.
-
April 28th, 2009, 02:58 PM #4
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
-
C and pointers help
By lost-and-found in forum Webmastering and ProgrammingReplies: 1Last Post: March 15th, 2009, 10:30 AM -
c++ pointers
By lost-and-found in forum Webmastering and ProgrammingReplies: 5Last Post: August 23rd, 2004, 08:21 PM -
Pointers?
By Brainchild in forum IMO CommunityReplies: 8Last Post: July 23rd, 2004, 06:58 PM -
Pointers as parameters...HELP!
By squeech in forum Webmastering and ProgrammingReplies: 1Last Post: September 16th, 2003, 10:45 PM -
C++ pointers
By Damien019 in forum Webmastering and ProgrammingReplies: 5Last Post: May 1st, 2003, 11:00 PM



LinkBack URL
About LinkBacks




Reply With Quote

is it working now? Sorry, I was at work on my lunch break when I replied earlier, so wasn't able to see your reply to follow up until now.
SLI problem? :(