Sign in with
Sign up | Sign in
Your question

More c++ questions. Getting object memory address.

Last response: in Applications
Share
a b L Programming
November 23, 2009 10:46:53 PM

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?
a b L Programming
November 24, 2009 2:10:28 PM

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.
m
0
l
a b L Programming
November 26, 2009 4:19:13 AM

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:
  1. void Add_Child(Word &child_to_add)
  2. {
  3. //in the first element of the array of child pointers, we
  4. //want to add the memory address of a child word object.
  5. childArray[emptyPointer] = &child_to_add;
  6.  
  7. //increase the emptyPointer variable
  8. emptyPointer++;
  9.  
  10.  
  11. //this will set the newly added child nodes "parent" to
  12. //the current word object
  13. child_to_add.Set_Parent(this);
  14. }
  15.  
  16. void Set_Parent(Word *parent_to_add)
  17. {
  18. parent = parent_to_add;
  19. }
  20.  
  21. /**
  22. * Returns the pointer to a child node at the desired position.
  23. **/
  24. Word Get_Child(int position)
  25. {
  26.  
  27. return *childArray[position];
  28. }
  29.  
  30. Word Get_Parent()
  31. {
  32. return *parent;
  33. }


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)?
m
0
l
Related resources
a b L Programming
November 26, 2009 2:33:13 PM

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?
m
0
l
a b L Programming
November 26, 2009 5:29:44 PM

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:
  1. void Add_Child(Word &child_to_add)
  2. {
  3. //in the first element of the array of child pointers, we
  4. //want to add the memory address of a child word object.
  5. childArray[emptyPointer] = &child_to_add;
  6.  
  7. //increase the emptyPointer variable
  8. emptyPointer++;
  9.  
  10.  
  11. //this will set the newly added child nodes "parent" to
  12. //the current word object
  13. child_to_add.Set_Parent(*this);
  14. }
  15.  
  16. void Set_Parent(Word &parent_to_add)
  17. {
  18. parent = &parent_to_add;
  19. }
  20.  
  21. /**
  22. * Returns the pointer to a child node at the desired position.
  23. **/
  24. Word & Get_Child(int position)
  25. {
  26. return *childArray[position];
  27. }
  28.  
  29. Word & Get_Parent()
  30. {
  31. return *parent;
  32. }


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:
  1. #include <iostream>
  2. #include "Word.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //word a
  8. Word::Word a ("a");
  9.  
  10. //the current node we will be working with
  11. Word::Word current = a;
  12.  
  13.  
  14. cout << "The Parent word is " << current.Get_Word() << endl;
  15.  
  16. //creates word "aa" and adds as a child of the current word
  17. Word::Word aa("aa");
  18. current.Add_Child(aa);
  19.  
  20. //switch current to the child node
  21. current = current.Get_Child(0);
  22.  
  23. cout << "the first child node is " << current.Get_Word() << endl;
  24.  
  25. //in theory, sets current back to the original "a"
  26. current = current.Get_Parent();
  27.  
  28. //tests to see what current is. It is still returning "aa", the child node
  29. cout << "the parent node is again " << current.Get_Word() << endl;
  30.  
  31. return 0;
  32. }

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.
m
0
l
a b L Programming
November 26, 2009 5:53:23 PM

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;"

    m
    0
    l
    !