More c++ questions. Getting object memory address.

kyeana

Distinguished
May 21, 2008
230
0
18,860
Hey everyone, I am back again (hopefully for the last time) :p

My question this time: is there a way to get the memory address of an object from within that object? (if that makes sense).

For example, i am building a tree, and have an Add_Child(Object& o) function. When this function is called, i want it to save a pointer to the newly added child (i have done this part, and it works), but also to set the newly added childs "Parent" to the current object.

What i am currently doing is i have another method Set_Parent(Object& o), and am calling that method something like this (it may not be exactly how i have it, sorry. I don't have the come in from of me):

newChild.Set_Parent(*this);

This will compile just fine, and i can navigate down the tree from parent to child, however, when i try to go back up the tree (from child to parent) i get a segmentation fault.

Any ideas? Thanks in advance :)


EDIT: I have done some more searching, and realized that i think i can just let the recursion handle moving back up through the tree (D'oh!) assuming i can build the tree using a recursive method (my brain hurts, ill tackle that one tomorrow). However, for future reference do you know if there is still a way to get the memroy from the current object?
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
If it can be of any comfort, you seem to be on the right track, probably a simple technical issue preventing your code from working.

"this" is actually a pointer, but if you do "*this", you actually dereference the pointer and the object will get copied to the stack which is then released when the function returns. Can you give us a bit more code so we can find the exact problem you are having (please use the [ code ] tags). But right now I would say that calling "newChild.Set_Parent(this);" would solve the problem.
 

kyeana

Distinguished
May 21, 2008
230
0
18,860
Ok, i used just this instead of *this (the method was originally expecting a reference, so i changed it to use a pointer instead) and now it compiles and runs without seg faults. However, now it seems that the pointer to the parent is holding.

The code is:
Code:
void Add_Child(Word &child_to_add)
		{
			//in the first element of the array of child pointers, we
			//want to add the memory address of a child word object.
			childArray[emptyPointer] = &child_to_add;
			
			//increase the emptyPointer variable
			emptyPointer++;
			
			
			//this will set the newly added child nodes "parent" to 
			//the current word object
			child_to_add.Set_Parent(this);
		}
		
		void Set_Parent(Word *parent_to_add)
		{
			parent = parent_to_add;
		}
		
		/**
		 * Returns the pointer to a child node at the desired position.
		 **/
		Word Get_Child(int position)
		{

			return *childArray[position];
		}
		
		Word Get_Parent()
		{
			return *parent;
		}

It looks like whats going on now is the "parent" pointer is pointing to itself instead of the actual parent. If i call child=child.Get_Parent(); then child.Get_Word();, it is still returning the word of the child instead of the parent. I'll keep looking at my code, but if you have any advice i would love to hear it. I can't decide if I'm just not thinking this through properly, or if I'm misunderstand something about pointers and references.

Thanks for the help :)

EDIT: Could it be a problem with me creating the objects on the stack, instead of in the heap (as a pointer)?
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
I didn't have a full view of your code, I didn't know you were using references (which is more of an OOP concept than pointer), I suggest you go back to using references then. Could you include a sample code to reproduce the problem?

One thing I noticed is that the Get methods will actually return a copy of the objects, not a reference, is that the wanted behavior?
 

kyeana

Distinguished
May 21, 2008
230
0
18,860
Hmm... thinking about this, in theory i believe that i should be returning a reference instead of a copy of the object, but im gonna do some testing to make sure i understand all of this correctly. Thanks for catching that.

How the code looks now:
Code:
void Add_Child(Word &child_to_add)
		{
			//in the first element of the array of child pointers, we
			//want to add the memory address of a child word object.
			childArray[emptyPointer] = &child_to_add;
			
			//increase the emptyPointer variable
			emptyPointer++;
			
			
			//this will set the newly added child nodes "parent" to 
			//the current word object
			child_to_add.Set_Parent(*this);
		}
		
		void Set_Parent(Word &parent_to_add)
		{
			parent = &parent_to_add;
		}
		
		/**
		 * Returns the pointer to a child node at the desired position.
		 **/
		Word & Get_Child(int position)
		{
			return *childArray[position];	
		}
		
		Word & Get_Parent()
		{
			return *parent;
		}

Now it seems to be mostly working (no seg faults), so obviously i changed something sense last time i was using references, but i don't know what it is right off of hand.

What this code is doing is setting the parent pointer equal to itself (i think) For example if this code is run:
Code:
#include <iostream>
#include "Word.h"
using namespace std;

int main()
{	
	//word a
	Word::Word a ("a");
	
	//the current node we will be working with
	Word::Word current = a;
	
	
	cout << "The Parent word is " << current.Get_Word() << endl;
	
	//creates word "aa" and adds as a child of the current word
	Word::Word aa("aa");
	current.Add_Child(aa);
	
	//switch current to the child node
	current = current.Get_Child(0);
	
	cout << "the first child node is " << current.Get_Word() << endl;
	
	//in theory, sets current back to the original "a"
	current = current.Get_Parent();
	
	//tests to see what current is. It is still returning "aa", the child node
	cout << "the parent node is again " << current.Get_Word() << endl;
	
	return 0;
}
The result is:
The Parent word is a
the first child node is aa
the parent node is again aa

This also got me thinking, currently i am using pointers to point to the children / parent nodes in the tree. Would it be better (and/or possible) for me to use references for the children/parents instead of pointers? If it can be implemented that way (and i imagine it can) it seems like that would make things simpler to read/understand compared to using pointers.
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
Seeing how complicated a simple thing is in C++ reminds me why I loved Java so much :p. I haven't figured-out (yet) what is wrong with the code, my C++ is a few years behind me and there is a lot of technical traps to be aware of when doing C++. Here is a few things you might want to investigate/consider:
■When you write "Word::Word current = a", I think it is the same thing as writing "Word::Word current(a)" which means the copy constructor is called.
■By default, I think a copy constructor is simply a memcpy(), which can be a problem because arrays are actually pointers (the value of the pointer would be copied, thus the 2 classes would reference the same memory addresses).
■One can override the "=" operator, I don't know exactly what the default implementation is.
■Try defining "current" as a reference (ie: "Word::Word & current = a;"