exterminator
July 14th, 2005, 02:30 AM
What are various types of managed pointers? Could you guys just point out some of the keywords that I could look for? I need to enhance my acquaintance with them to an advanced level. Thanks.
|
Click to See Complete Forum and Search --> : Managed Pointers exterminator July 14th, 2005, 02:30 AM What are various types of managed pointers? Could you guys just point out some of the keywords that I could look for? I need to enhance my acquaintance with them to an advanced level. Thanks. Siddhartha July 14th, 2005, 04:14 AM Could you guys just point out some of the keywords that I could look for? I need to enhance my acquaintance with them to an advanced level.Do you mean smart pointers? If so, look at - www.boost.org (http://www.boost.org) Boost Smart Pointers (http://www.boost.org/libs/smart_ptr/smart_ptr.htm) (Reference counted smart pointers) Or do you mean garbage collected "managed" objects (as in .NET)? upashu2 July 14th, 2005, 04:26 AM I think you mean __gc pointers in .NET. If it is Then you may look __gc pointer or managed pointer in MSDN. What are various types of managed pointers? __gc pointers are just pointer to managed objects, in other words, they point to managed block of memory in common language runtime heap. You may see also pinned pointers - _pin __gc pointers on MSDN. One more interesting thing about __gc pointers is that you need not initialize them with NULL, compiler and runtime make sure to initialize them with zero. NMTop40 July 14th, 2005, 05:22 AM There are different varieties of managed pointers. The simplest is std::auto_ptr although it is limited in its use and boost::scoped_ptr is probably better for the purpose. Smart-pointers generally take care of the deletion of the pointer after use, so you actually call new but don't call a matching delete. - Scoped pointers are simply held by one smart-pointer object and are the pointer is deleted at the same time as the object. Scoped pointers cannot be copied, they can be reseated but using reset() to another pointer, not operator=() (either to a pointer or a scoped pointer). You might be able to do a "swap" between scoped pointers. - Shared pointers use reference counting. Two shared-pointers can point to the same object. They are useful in collections and as return values from functions. Beware though if you use them as a class member that when your class is copied the copy gets a copy of the shared-pointer but they both point to the same object. - COW pointers (copy-on-write) also use reference-counting as above, but if there are 2 or more instances and someone tries to modify the pointer, then the object will be "cloned". Beware also that const shared_ptr< T > & is a const pointer, not a pointer to a const T. You can however have a shared_ptr< const T > and a good implementation of shared_ptr will allow you to copy from shared_ptr< T > to shared_ptr< const T > implicitly but not the other way round. Similarly you can copy implicitly from shared_ptr< Derived > to shared_ptr < Base > where Derived is derived from Base. boost::shared_ptr has both of these features, and also has special cast operators to convert the other way. You may also want to give your pointer a custom deleter. boost does have shared_array (I thnk) but you don't actually need it, you can use shared_ptr with a custom deleter (that uses delete[] instead of delete). One main reason for using a custom deleter is where you have a DLL / shared-object that creates an object and you want to use a function on the same library to delete it. (Beware though of your library returning a boost::shared_ptr itself, because you and your clients might be using different versions). boost does not (yet) offer a COW pointer (as far as I know) but I think Loki does. I don't know about .NET but COM uses intrusive shared-pointers, and ATL provides templates for them (CComPtr). exterminator July 14th, 2005, 07:11 AM EDIT: Aaahh, there seems to be some problem with my internet connection or the codeguru database. I posted this message nearly 3 hours back and it got reflected now. My earlier message: Yes..smart pointers, auto pointers ..and may be others, I am not familiar with them (would like to know the names atleast of the various others). Yes, even garbage collectors would also be fine but not on .Net. I mean something in native C++ which imitates Garbage collection methodology. Sid, thanks for the links! Upashu2 - Thanks but I was not asking in respect to the .Net technologies. Apologies for not being clear in my OP. NMTop40 - Nice. Thanks. That gives a headstart. Guys, if you have anymore inputs..just pour in. I know I dont have some specific question but lets see how it goes! Thanks all ! NMTop40 July 14th, 2005, 07:35 AM C++ focuses on a technique known as RAII (resource acquisition is initialisation) rather than garbage collection. With RAII, you use the "destructor" feature of a class to release resources. And you take advantage of the fact that destructors get called automatically: 1. For an auto-variable at the end of its scope block (including if an exception is thrown). 2. For a class member variable when its containing class is destructed. Note that "scope" is often a misused word, associated with lifetime. For auto-variables they are the same thing, but for heap-allocated variables they are not. void afunc() { T t1; T * t2 = new T; T * t3 = new T; delete t2; // some more code } t1 is an auto-variable and the destructor is called at the end of the function afunc(), the same time that it goes out of scope. t2 is a pointer to a T, but its lifetime (the object it points to) ended earlier than its scope, when delete was called. t3 is a pointer to a T but its lifetime (the object it points to) still exists even after the pointer to it (t3) is no longer in scope. As there is possibly no way to retrieve this pointer it will cause a memory leak. (If T is implemented in some way such that its constructor adds itself to a collection controlled by a singleton, then there is a pointer to it somewhere). The concept of using smart-pointers is to protect yourself from memory leaks (having pointers like t3 disappearing without deleting them) and using "dangling" pointers (like t2 before the end of the function). Axter September 13th, 2005, 12:31 PM boost does not (yet) offer a COW pointer (as far as I know) but I think Loki does. Here's a simple version of a COW smart pointer: http://code.axter.com/cow_ptr.h Also clone pointers: http://code.axter.com/clone_ptr.h http://code.axter.com/copy_ptr.h And a synchronized smart pointer: http://code.axter.com/sync_ptr.h codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |