I am looking for a garbage collection library for C++ , and some documentation on how to use , Google search produced one by HP , however not sure how to use it.
IF some has better suggestion like smart pointer then please feel free to comment ,
I am currently working on the project ( developed by some one else ) it involves allocating lot of object pointers in vector array , and keeping track of all of them , I was essentially looking for a Java type solution ( if one exists ) to when an Object is no longer referenced by a pointer it get deleted automatically as oppose to manually deleting it.
I guess it sames on a lot of typing and careless errors.
I have heard of smart pointers however not sure how to use them.
I was essentially looking for a Java type solution ( if one exists ) to when an Object is no longer referenced by a pointer it get deleted automatically as oppose to manually deleting it.
Thanks Paul , could you please provide example on how this is used
Why not do a google search? The shared_ptr is now a part of standard C++, and explanations of how to use it are in abundance.
Code:
#include <memory>
struct foo
{
int x;
foo() : x(10) {}
};
typedef std::shared_ptr<foo> foo_ptr; // or std::tr1::shared_ptr if using VC 2008
int main()
{
foo_ptr f; // shared pointer not pointing to anything
// create a local scope for demo purposes
{
foo_ptr f1(new foo()); // create shared pointer and point it to a foo object, reference count is 1
f = f1; // now we have two pointers pointing to a foo, f and f1, so reference count is now 2
} // f1 goes out of scope -- reference count is decremented and is now 1.
} // Now f has gone out of scope. reference count is decremented
// and is now 0, so "delete" will be called automatically.
In other words, you are guaranteed to have the memory cleaned up when the last pointer pointing to the dynamically created object goes out of scope.
Smart pointers are a good alternative I think. But if you have a Java background there's a limitation you should know about. Smart pointers cannot handle circular chains. If an object holds a smart pointer to an object this object may not hold a smart pointer back, not directly nor indirectly.
Also, but this is secondary, since smart pointers make indirect accesses and since they keep a counter updated they're somewhat slower than ordinary bald pointers. I'm using smart pointers quite frequently and I've had an issue with this once only.
When I first graduated from Java to C++ I used smart pointers everywhere because they felt so familiar. Today I'm limiting their use mostly to publicly visible objects lacking an obvious owner. I find manual object management to be perfectly fine in implementation style code.
Finally I would like to recommend Intel TBB. It's not only great for writing multi-core programs, it also comes with an efficient memory allocation replacement,
But if you have a Java background there's a limitation you should know about. Smart pointers cannot handle circular chains. If an object holds a smart pointer to an object this object may not hold a smart pointer back, not directly nor indirectly.
regarding reference counted smart pointers, this issue can be solved by using weak_ptr in conjunction with shared_ptr, so the claim that "Smart pointers cannot handle circular chains" is a bit too strong; moreover, there's also unique_ptr which forbids circular references from the beginning ( note that the OP speaks of "object pointers in vector array", so, he doesn't necessarily need a shared reference semantics ... )
anyway, I agree that a liberal use of shared_ptr as a blind replacement of object pointers/references is the wrong thing to do, although I cannot imagine a different reason for this to be assumed other then coming from a Java background ...
anyway, I agree that a liberal use of shared_ptr as a blind replacement of object pointers/references is the wrong thing to do, although I cannot imagine a different reason for this to be assumed other then coming from a Java background ...
Sure, having a Java background is the ultimate liability for a C++ programmer.
Although reference counting smart pointers is an acceptable solution it doesn't beat full garbage collection. I wish C++ was extended to provide it. The C++/CLI language is a nice proof of concept. But it must be in the good old C++ tradition of course; Using it is voluntary and you don't pay if you don't use it.
Last edited by nuzzle; September 18th, 2011 at 04:29 AM.
Bookmarks