pointer and smart pointer address

This is a discussion on pointer and smart pointer address within the C++ Programming forums, part of the General Programming Boards category; Hello.. I want to get address of smart pointer but I have no idea how to do that - I ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    pointer and smart pointer address

    Hello..

    I want to get address of smart pointer but I have no idea how to do that - I need a unique key for each class instance..
    I use boost smart pointer.

    The way I would do it with normal pointer:

    Code:
    class someclass {
    public:
    	someclass() { }
    	char test;
    	bool yes;
    };
    
    
    typedef boost::shared_ptr<someclass> someclass_p;	//smart pointer
    
    void main() {
    	someclass *test = new someclass();
    	DWORD bu = (DWORD) test;
    	std::cout << "pointer: " << bu << "\n" ;
    	
    	someclass_p testp = someclass_p( new someclass() );
    	//How do I get address here?
    }
    How to achieve this? Is this a 'good' way to do it?
    Thanks a lot for help

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    30

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    According to the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Don't use void main(). Use int main().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks guys..

    One more question.. How can I get smart pointer when I have its address?
    Example:

    Code:
    class someclass {
    public:
    	someclass() { }
    	char test;
    	bool yes;
    };
    
    typedef boost::shared_ptr<someclass> someclass_p;	//smart pointer
    
    int main() {
    	someclass *test = new someclass();
    	DWORD bu = (DWORD) test;
    	std::cout << "pointer: " << bu << "\n" ;
    	
    	someclass_p testp = someclass_p( new someclass() );
    	ULONG_PTR uptr = (ULONG_PTR)testp.get();
    	//so I have pointer address here
    	//How do I get testp from uptr?
    	
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can't. The return value of get() is the internally stored raw pointer location. It has no information about any smart pointers that manage it.

    Just to clarify, if there is more than one shared_ptr sharing a someclass instance, do you want each shared_ptr to return a different address, or should it be the same for all shared_ptr's containing the single someclass instance?

    Depending on why you need the address, there are other options. For example, if you are just trying to store shared_ptr objects in a map, you can create a comparison function that compares shared_ptr<someclass> objects by comparing the return value of get(). You would still store the shared_ptr, so you would always have it available, but the unique key would be based on the internal pointer.

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved
    Depending on why you need the address, there are other options. For example, if you are just trying to store shared_ptr objects in a map, you can create a comparison function that compares shared_ptr<someclass> objects by comparing the return value of get(). You would still store the shared_ptr, so you would always have it available, but the unique key would be based on the internal pointer.
    This is actually what I thought of.. And seems the best way to solve my problem..

    Why I want to do this?
    I pass pointer address (of someclass) to windows api, which will return the address when something happens with this class (instance).
    Then I call functions to process data inside that class.

    It is more complicated if I use smart pointers instead of normal pointers.

    Would it be okay to call functions with raw pointer instead of shared/smart pointer?
    Wouldnt that lose the benefit of smart pointers?

    If I decide for option you mentioned, would map slow down preformance of my program? There will be like 10 000+ instances of someclass.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    On a side note, you are assigning new *test but not calling delete or setting the pointer to NULL when the peogram terminates. Becareful of this, dont create dangling pointers.
    I may have this wrong assumsion, as that could just be example code, but I am going on what I see...
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You need something to own the pointers. A container of shared_ptr's is great for that. If you pass the raw pointer to the windows API it will be fine to call member functions from the raw pointer instead of the shared_ptr. That will not lose any smart pointer benefits because the smart pointer is meant only to help manage the memory/construction/destruction of the object.

    How are you storing the shared_ptrs now? Are they all in a container already? Are they members of other classes?

    One option is to have an std::set<shared_ptr<someclass>, CompareByRawPointer> where CompareByRawPointer is a comparison function for shared_ptr<someclass> objects that uses the raw pointer. This should not slow down your performance too much, as lookup is fast in a set. You could also use a unordered_set if you were really concerned about it, since a unordered_set is potentially much faster than a set. Finally, if you create all your shared_ptrs first, you could use a vector, then sort it after all objects have been inserted: std::sort(vec.begin(), vec.end(), CompareByRawPointer). With all these options you can find the shared_ptr from the raw pointer if you really need to without much of a performance hit, bt you shouldn't need to do that excpet for when you want to delete the object.

  9. #9
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved
    How are you storing the shared_ptrs now? Are they all in a container already? Are they members of other classes?
    Right now I store shared_ptrs in a vector which is a member of another class.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can call member functions from the raw pointer without any issues. Is there any reason you want to get the shared_ptr from the raw pointer? The only one I can think of would be to remove an item from that vector when all you have is the raw pointer. If you never have to do that, then I don't think you should worry about it.

  11. #11
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved
    You can call member functions from the raw pointer without any issues. Is there any reason you want to get the shared_ptr from the raw pointer? The only one I can think of would be to remove an item from that vector when all you have is the raw pointer. If you never have to do that, then I don't think you should worry about it.
    I think I will have to locate smart pointer anyway, so I will be able to remove it from container. One function would remove the item from the cointainer (vector) and the others just process the data inside the item (class).

    So actually even if I call functions with raw pointer I wont have benefit, because I will have to locate the smart pointer anyway to remove the item from container when its needed?
    So I just have to focus on most efficient container and optimize it..

    Please correct me if I'm wrong.

    Thanks for help again

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> So actually even if I call functions with raw pointer I wont have benefit, because I will have to locate the smart pointer anyway to remove the item from container when its needed?

    Still, the lookup of the smart pointer will take some processing that is not necessary. It sounds like you still need to provide the ability to look up the smart pointer, but you should only actually do it if you need to remove the object. Otherwise, use the raw pointer directly.

    Looking up the smart pointer to call the member functions has no benefits and so you should just not do it.

  13. #13
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Hey Daved, thanks for your help.. You're very helpful..

    One more thing.. How could I find the right smart pointer if I decide to go for std::set? Map would work just perfectly but I am worried about the preformance.

    Thanks again

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Map would work fine, but it is an unnecessary duplication of data (the raw pointer would be stored twice). If that seems a lot more intuitive for you, use the map. The performance hits are unlikely to be noticable, even with 10,000+ objects.

    For a set to work you would need to provide your own compare function like I mentioned above. A simple version would be a function returning bool and taking two shared_ptr<someclass> references to const. That function would just return true if the first parameter's get() was less than the second, otherwise false. This would sort the shared_ptr's in the set based on the raw pointer value, so you wouldn't need the map. You'd still have to figure out how to use a raw pointer to lookup the value in the set. Normally you'd construct a temporary shared_ptr with the raw pointer, but I'm not sure if that would accidentally delete the object. You'd have to do some research into how shared_ptr works to make sure if you really want to go this route. Otherwise stick with the map.

  15. #15
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thank you.. Map seems the best and easier option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which smart pointer to use?
    By leeor_net in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 05:29 AM
  2. Smart pointer questions
    By Elysia in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2009, 02:54 PM
  3. smart pointer release
    By George2 in forum C++ Programming
    Replies: 24
    Last Post: 02-13-2008, 08:51 AM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  5. Using a smart pointer for all objects
    By phalc in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 09:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21