C ++ Learning Strengthening and Smart Pointers

I am doing a Masters project on robotic sensorimotor online learning using reinforcement training methods (Q, sarsa, TD (Ξ»), Actor-Critic, R, etc.). I am currently developing a framework that will use both training with a higher level of training and the management of a lower level robot API.

Since the states depend on the robot sensor and can (will) increase exponentially, I will allocate them in a heap. Since this can create a lot of problems, errors, etc., And since parallelization (i.e. threads) is an aspect of gain training that I want to learn, I'm not sure which smart pointers to use.

Designing my own template / class for a smart pointer will take time and debugging, which I don't have. So, I am wondering if I should use STL auto_ptr ? I see that they have problems used in vectors. Should I use boost::shared_ptr ? States must be divided between many classes and algorithms. Or use boost::ptr_vector ? Since the states will be in the task container class in the vector, is this enough? States must be divided, copied, referenced, serializable, not permanent, thread safe and will not be deleted. In addition, memory space and computation time are important.

What do you recommend as the best smart ptr implementation for such a task?

Thanks!


It looks like I will have to try using boost :: ptr_vector with the State class, and if that turns out to be inefficient, use std :: vector <std :: unique_ptr> and turn on 0X. Thank you all for your answers and suggestions!

+6
source share
3 answers

Single-owner pointers are more difficult to use, modulo std::auto_ptr . You can use boost::scoped_ptr , which is even more secure (it cannot transfer ownership, although it cannot be returned by a function). When it comes to containers, you can use a pointer container, but it is also suitable for use, for example. std::vector without smart pointers, if the types used are not used polymorphically.

Co-ownership should remain an exceptional case; do not abuse boost::shared_ptr .

+4
source

Other suggestions missed one. boost::intrusive_ptr works better than shared_ptr because a second selection is not required to store the reference count. The disadvantage of intrusive_ptr is the tiny bit of extra bookkeeping and the weak_ptr to use weak_ptr .

+1
source

I never found the smart ptr class that I liked when I was doing C ++. In the end, I wrote my own.

It had a cache function for speed, so you continued to allocate and free memory that it could just hang on the memory that it had and reuse. In addition, it did not have a default constructor, so you had to pass its functions / methods by reference, otherwise the compiler will show an error, so it did not create a temporary copy of memory, especially when working with large image files.

You don’t need to write your own code for too long, and you can also add your own border checking code.

+1
source

Source: https://habr.com/ru/post/892180/


All Articles