home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ Array of Pointers

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2428
Discussions: 200,941, Posts: 2,379,277, Members: 246,304
Old April 25th, 2009, 05:00 PM   Digg it!   #1 (permalink)
分かりますか。
 
carl33p's Avatar
 
Join Date: Feb 2006
Location: Gville, FL
Posts: 7,156
Blog Entries: 9
Send a message via AIM to carl33p
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 05:06 PM.
carl33p is offline   Reply With Quote
Old April 25th, 2009, 06:11 PM     #2 (permalink)
分かりますか。
 
carl33p's Avatar
 
Join Date: Feb 2006
Location: Gville, FL
Posts: 7,156
Blog Entries: 9
Send a message via AIM to carl33p
I found a work around for creating an array of pointers to TreeNodes.

But Im still not sure about this test program.
carl33p is offline   Reply With Quote
Old April 25th, 2009, 08:36 PM     #3 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
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.
sr71000 is offline   Reply With Quote
Old April 28th, 2009, 03:58 PM     #4 (permalink)
分かりますか。
 
carl33p's Avatar
 
Join Date: Feb 2006
Location: Gville, FL
Posts: 7,156
Blog Entries: 9
Send a message via AIM to carl33p
I see. That explains a lot. Thanks.

Been using Java too much...
carl33p is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Is It Just Me? (2916)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Wireless Televisions. (9)
California Passes Anti-Flat-HDTV Le.. (43)
Regular Build (6)
Obama the Muslim (14)
Is the PSU I received dead? (11)
HIS HD5770 graphic card question (15)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
Wireless Televisions. (9)
radeon x850xt platinum & shader 3 (4)
screen resolution vs monitor size (1)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Is the PSU I received dead? (11)
Have you switched yet? (85)
Point and Shoot Camera Suggestions. (2)
Modern Warfare 2 freeze (13)
wireless user (1)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Fire in DVD (2)
Modern Warfare For the PC (33)


All times are GMT -4. The time now is 02:43 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28