Can I use C ++ Boost shared_ptr for programming, as if I were coding in Java, like, without worrying about memory management?

It was some time that I was encoded in C / C ++, and now I need its effectiveness for the project that I am doing.

What I understand from this shared_ptr is that it basically deletes the object when I need it. So, if, for example, my object has a shared_ptr vector, then I don’t have to worry about iterating through the vector and deleting each element in the destructor? In other words, I do not need to worry about memory management while I use them? Or do I absolutely not understand this? That sounds too good to be true.

+6
java c ++ memory-management boost
source share
3 answers

You must understand that generic pointers are implemented using a reference counter, which means that if you have loops in your pointer graph, objects will not be released. That is, if points b and b point to a, but nothing points to a or b, then neither one nor b will be freed, because they both have a reference count of '1'.

Boost provides weak pointers to get around this, which allows you to store a pointer to a shared object without increasing its number of references. Weak pointers provide a level of security in that trying to dereference a pointer after releasing a shared pointer will throw an exception rather than a program crash.

Generic pointers are also quite expensive (at least compared to the raw pointer) in terms of performance - but it's better to use them and then delete them as soon as the profiler identifies a bottleneck rather than using them everywhere.

In addition, yes, they are very useful for managing dynamically allocated objects.

Edit: Another problem (mentioned on the promotion pages) is to avoid the “temporary” shared_pointers:

func(A(), boost::shared_ptr<B>(new B)); 

because the compiler is allowed to optimize it as

 tmp1 = new B; tmp2 = A(); tmp3 = boost::shared_ptr<B>(tmp1) func(tmp2,tmp3) 

which may look normal at first glance, but if A () throws an exception, then B was thrown, but shared_ptr had not yet received it, so the pointer is never freed.

+11
source share

You can do it, but it’s generally a bad idea. For starters, you may lose some or all of the efficiency that you think you can gain.

More importantly, it looks like you are trying to avoid code development. Java has gc, so you don’t have to worry about memory management, but you still have to worry about the life of objects. If you don’t know who owns what you are likely to end up with a confusing design.

C ++ gives you many options when it comes to the life of objects, and not every complex object should be allocated on the heap. shared_ptr should be used for objects that require joint ownership (as the name implies), but it should be a positive design decision. There are more effective ways of owning an object if joint or transferable ownership is not required.

+6
source share

Here Is boost :: shared_ptr <T> equivalent in C #? I posted some information about various auto memory management strategies. This explains the main differences between GC and reference counting.

Only a few modern C ++ developers use things like manual memory (or resource) management. Most of us use the RAII idiom instead (see http://en.wikipedia.org/wiki/RAII ), that is, some utility classes that help us manage resources.

Shared_ptr is a great way that makes it easier to complete memory / resource management tasks. This method has some drawbacks (it cannot deal with cyclic structures, and it has an overhead of performance both in time and in space. For each operation on links, the implementation now performs an arithmetic operation - and if disconnected, a conditional instruction. Besides in addition, each object must be expanded with an additional field for storing the counter).

I recently participated in a rather large project (only my part contains more than 2 hundred classes), and I never (NEVER) used the delete operator manually. Such utilities make coding, debugging, maintenance easier and significantly reduce development costs. Recently, I have been participating in a similar project (with very similar business logic and architecture), but in C #. And I can tell you for real: they are very similar (first of all, because automatic memory management in a C ++ project).

PS Not every C ++ programmer knew, but shared_ptr can use in automatic resource management (not only in automatic memory management) with user deletion.

+4
source share

All Articles