C++ Array of Pointers  | |
April 25th, 2009, 05:00 PM
|
#1 (permalink)
| | 分かりますか。
Join Date: Feb 2006 Location: Gville, FL
Posts: 7,156
|
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:
Last edited by carl33p : April 25th, 2009 at 05:06 PM.
|
| |
April 25th, 2009, 06:11 PM
|
#2 (permalink)
| | 分かりますか。
Join Date: Feb 2006 Location: Gville, FL
Posts: 7,156
|
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, 08:36 PM
|
#3 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
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 08:42 PM.
|
| |
April 28th, 2009, 03:58 PM
|
#4 (permalink)
| | 分かりますか。
Join Date: Feb 2006 Location: Gville, FL
Posts: 7,156
|
I see. That explains a lot.  Thanks.
Been using Java too much... |
| | | Thread Tools | Search this Thread | | | |
Similar Threads | | Thread | Thread Starter | Forum | Replies | Last Post | | C and pointers help | lost-and-found | Webmastering and Programming | 1 | March 15th, 2009 11:30 AM | | c++ pointers | lost-and-found | Webmastering and Programming | 5 | August 23rd, 2004 09:21 PM | | Pointers? | Brainchild | IMO Community | 8 | July 23rd, 2004 07:58 PM | | Pointers as parameters...HELP! | squeech | Webmastering and Programming | 1 | September 16th, 2003 11:45 PM | | C++ pointers | Damien019 | Webmastering and Programming | 5 | May 2nd, 2003 12:00 AM | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |