4

I am writing a class for my SLAM algorithm and this is my first large C++ project! I do remember that std::unique_ptr should be used when I want to keep some object which should have a dynamic memory, one owner and a long lifetime. So when designing a specific class which its object is being created only once and should have a global lifetime (it is the core class object that holds the map). So my idea was to create the std::unique_ptr which will hold that object:

class Backend
{
private:
    std::vector<double> values;
    /// some members
public:
    Backend() : values{0} {}
    ~Backend(){}
    // some functions
};

auto backend_ptr = std::make_unique(Backend());

So my question is: Does the size of backend_ptr will grow if I will increase the size of its private member values overtime? And with your suggestion, do I even need this unique_ptr at all?

aikhs
  • 949
  • 2
  • 8
  • 20
  • 2
    It is not possible to increase size of `values` member. It is defined at compile time and is not related to the value returned by `values.size()`. At the same time size of `backend_ptr` is not related to the size of `Backend` and is defined at compile time as well. – user7860670 May 19 '19 at 19:22
  • It's a bit unclear what yoou're asking about. An expression like `sizof(Backend)` will be stable, and determined at compile time. – πάντα ῥεῖ May 19 '19 at 19:23
  • @VTT I thought vector has non-static memory which can increase or shrink overtime – aikhs May 19 '19 at 19:24
  • ***Does the size of backend_ptr will grow if I will increase the size of its private member values overtime?*** No, this is a compile time constant. – drescherjm May 19 '19 at 19:24
  • ***I thought vector has non-static memory which can increase or shrink overtime*** The vector can allocate the data it has on the heap will but that has nothing to do with the size of `Backend` or `backend_ptr` or even the sizeof `values`. – drescherjm May 19 '19 at 19:25
  • @Edmund Yes, it has. But it does not effect size of vector object, or size of objects containing that vector as a field or size of objects wrapping a pointer to the object containing that vector as a field. – user7860670 May 19 '19 at 19:26
  • @drescherjm I see, so that is just another memory allocation which is runtime? – aikhs May 19 '19 at 19:26
  • The vector will contain a pointer to dynamically allocated memory. The size of the pointer does not change. – drescherjm May 19 '19 at 19:28
  • @VTT Ok, so in that case declaring a unique_ptr is totally unnecessary in my case? – aikhs May 19 '19 at 19:29
  • @drescherjm Yeah, now I understand what you are saying – aikhs May 19 '19 at 19:29
  • Do yourself a favor and read a good [C++ Book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You'll save a ton of time (seriously). – rustyx May 19 '19 at 19:33
  • @rustyx Ok, cheers. I do read books about C++ but when I think I got a good grasp of it sometimes I get another view of topics over SO and it gets me confused – aikhs May 19 '19 at 19:37
  • Probably. Also it should've been `std::make_unique();` otherwise there will be an unnamed temporary `Backend` object. – user7860670 May 19 '19 at 19:39
  • I did make one mistake in my comments above. The mention of the word **heap**. Officially c++ does not mention a heap instead it has **dynamic storage duration** see here: https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and – drescherjm May 19 '19 at 19:58

1 Answers1

5

The size of the object is determined during the compilation and has nothing to do with the dynamically allocated memory of your std::vector values. This is because during the compilation, your compiler will allocate a fixed memory for the pointers which will point to your variables and objects. So in your case, the pointer to your std::vector.

Regarding whether it is feasible to use std::unique_ptr, it is hard to say without seeing the overall design but my personal choice would be to avoid in your case.