Good practice or convention for a heap of dedicated cleanup object?

I am learning C ++. I have C, C #, ObjC background. Pretty high-level languages.

In C # or ObjC, this is a trivial returned heap of a selected object as a result of a function or method. Because the cleaning of objects is managed (by agreement). It will be destroyed in due time.

But I do not know how I should deal with this in C ++.

For instance,

std::string* makeString() { std::string* str = GetSomeStringFromArbitrarySource (); SaveSomewhereElseInternally (str); return str; } void useString () { std::string* str = makeString (); // Where and how should I cleanup the `str` object? // It is not safe `delete str` here because it may be used on another place. } 

What is the recommended and usual way to clear a heap of a selected object when it is passed through many functions?

I looked at a few smart pointers, but they don't really look like reducing complexity or things to care for. Am I misunderstanding smart pointers?

+4
source share
5 answers

If you can use C ++ 11, use generic pointers . These pointers implement a mechanism to delete the selected object after the destruction of the last of them. If you are using C ++ 03, use boost generic pointers . If you cannot use either, try moving the heap allocation to the classes that you allocate on the stack and then passing references to them, also let the RAII wiki read.

0
source

I looked at a few smart pointers, but they don't really look like reducing complexity or things to care for. Am I misunderstanding smart pointers?

Probably yes. In C ++, since you have to deal with it yourself (GC will not clear it for you), you need a way to track the usage of each object.

You can manually map each new to delete , but it is sometimes difficult or impossible, for example, in your script above. It is not possible to find out if this object is being used elsewhere.

Smart pointers solve this by managing the lifetime for you, so you don't need to delete. They use various mechanisms to track the number of places the object is used, and call delete when the last one is executed.

However, in this particular case there are not many reasons to use pointers. If you work with std::string , you can pass a string by value, and that will never be a problem.

+3
source

You need to find out the desired lifetime for your objects, and then use a type system to ensure that this lifetime is respected.

In your example, this is not entirely clear if you want the object to be destroyed.

In C ++, usually values ​​are passed to objects by value (since primitive types are found in Java / C # / etc), so if you do not need to share std::string between different code fragments, the usual thing would be to simply return the string by value (write makeString as std::string makeString() ).

If you need to have several places that reference the same object, you should carefully think about the design and decide which part of the program can safely control the lifetime of the object. Create an object by value in this place, and then pass pointers and links elsewhere.

+2
source

You misunderstand smart pointers, in all likelihood, for the same reason why you wrote:

 std::string* makeString() 

Instead of what most C ++ programmers would write:

 std::string makeString() 

You would need to better understand the lifetime of an object in C ++ , and then the concept of smart pointers would be much simpler.

+2
source

You may misunderstand smart pointers. In C ++, an alternative is that the programmer needs to track the lifetime and use of dynamically distributed objects. This affects the design of the software and also leads to user errors. When you use smart pointers, the life of the asset takes care of you pretty much.

I was raised with the classic C ++ style (without smart pointers), and so I can program this way if necessary, but if you start with C ++, then smart pointers are really required.

+1
source

All Articles