Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing shared_ptr to managed language references

Tags:

java

c++

c#

Could someone explain to a C++ programmer most important differences between Java (and C# as well) references and shared_ptr (from Boost or from C++0x).

I more or less aware how shared_ptr is implemented. I am curious about differences in the following ares:

1) Performance. 2) Cycling. shared_ptr can be cycled (A and B hold pointers to each other). Is cycling possible in Java? 3) Anything else?

Thank you.

like image 974
watson1180 Avatar asked Dec 31 '10 15:12

watson1180


4 Answers

Performance: shared_ptr performs pretty well, but in my experience is slightly less efficient than explicit memory management, mostly because it is reference counted and the reference count has to allocated as well. How well it performs depends on a lot of factors and how well it compares to Java/C# garbage collectors can only be determined on a per use case basis (depends on language implementation among other factors).

Cycling is only possible with weak_ptr, not with two shared_ptrs. Java allows cycling without further ado; its garbage collector will break the cycles. My guess is that C# does the same.

Anything else: the object pointed to by a shared_ptr is destroyed as soon as the last reference to it goes out of scope. The destructor is called immediately. In Java, the finalizer may not be called immediately. I don't know how C# behaves on this point.

like image 189
Fred Foo Avatar answered Oct 15 '22 02:10

Fred Foo


The key difference is that when the shared pointer's use count goes to zero, the object it points to is destroyed (destructor is called and object is deallocated), immediately. In Java and C# the deallocation of the object is postponed until the Garbage Collector chooses to deallocate the object (i.e., it is non-deterministic).

With regard to cycles, I am not sure I understand what you mean. It is quite common in Java and C# to have two objects that contain member fields that refer to each other, thus creating a cycle. For example a car and an engine - the car refers to the engine via an engine field and the engine can refer to its car via a car field.

like image 42
Michael Goldshteyn Avatar answered Oct 15 '22 01:10

Michael Goldshteyn


Nobody pointed the possibility of moving the object by the memory manager in managed memory. So in C# there are no simple references/pointers, they work like IDs describing object which is returned by the manager.
In C++ you can't achieve this with shared_ptr, because the object stays in the same location after it has been created.

like image 2
SOReader Avatar answered Oct 15 '22 00:10

SOReader


First of all, Java/C# have only pointers, not references, though they call them that way. Reference is a unique C++ feature. Garbage collection in Java/C# basically means infinite life-time. shared_ptr on the other hand provides sharing and deterministic destruction, when the count goes to zero. Therefore, shared_ptr can be used to automatically manage any resources, not just memory allocation. In a sense (just like any RAII design) it turns pointer semantics into more powerful value semantics.

like image 1
Gene Bushuyev Avatar answered Oct 15 '22 00:10

Gene Bushuyev