1

I'm trying to have a struct take possession of a pointer to an array of pointers to objects on the heap. In order to not have to worry about deleting these objects I want to use smart pointers. However, I can't seem to find a correct syntax to do so...

This is what (I think) I want to achieve:

    const std::unique_ptr<(*Position)[]> entity_position;

And then receive a unique_ptr<*Position[]> as a parameter of the struct constructor. But I am getting an expected expression error on the closing ']'. I have looked at std::unique_ptr with array pointer (to confirm delete[] would indeed be called) and Get array of raw pointers from array of std::unique_ptr but I haven't fully understood what this last one was trying to achieve.

Here is a simplified version of the code I am writing:

#include <memory>
#include "Position.hpp"
struct myStruct final : public baseStruct{
    const std::unique_ptr<(*Position)[]> pos_pointer_array;

    myStruct(std::unique_ptr<(*Position)[]> ptr_to_get): pos_pointer_array(ptr_to_get){}

}

Any help much appreciate ! (And disclaimer: I haven't really used smart pointers before...)

4
  • Position *[] should work. Commented Feb 24, 2021 at 12:09
  • Who owns the objects pointed to by the raw non-owning pointers that will in the array? Commented Feb 24, 2021 at 12:29
  • @Eljay it's bad, but I was going for no-one, they are created after reading data from a file and as such I thought if I could get the smart pointer to automatically deallocate the memory I wouldn't need anyone to own it. Commented Feb 24, 2021 at 12:36
  • The smart pointer will delete the allocated array of non-owning raw pointers. The smart pointer does not own the objects those non-owning raw pointers point to. The answer posted by largest_prime_is_463035818 sounds like what you intended. Commented Feb 24, 2021 at 14:12

2 Answers 2

3

A unique_ptr to an array will only clean up the array, but if the elements are pointers to objects stored elsewhere (presumably on the heap) then those objects are not cleaned up automagically.

It seems you rather want a

 std::vector< std::unique_ptr<Position>> 

or a unqiue_ptr to an array with a custom deleter that also deletes the elements. On the other hand, if the array owns the Positions chances are high that all you need is a

 std::vector<Position>
Sign up to request clarification or add additional context in comments.

3 Comments

I was going for an array of Position* because I was thinking to allocate them on the heap (as correctly presumed) and directly storing the pointers to this memory in the array. If I'm not mistaken having a std::vector<Position> would cause a copy of the Position object to be made when putting it into the array ? But in that case std::vector< std::unique_ptr<Position>> does seem like a great idea !
@A.D a vector does store its elements in dynamically allocated memory. It does "own" its elements, which means, yes, if you insert a Position into a std::vector<Position> then the element in the vector will be a copy. Though, thats not really an issue, if Position can be moved, and because elements can be constructed in place via emplace. A std::vector<Position> is almost always the better choice compared to a std::vector<Position*> (unless the elements are actually not owned by the vector)
I... Have to admit I totally forgot about move transfer on this one -_- But yes, in that case I can totally see why std::vector<Position> would be favored !
2

As noted by IlCapitano:

Position *[] should work.

A pointer to Position is Position*.

An array (of unknown size) of Position* is Position*[].


But you should check whether you really need this. Maybe you want std::unique_ptr<Position[]>? This is the usual case of a smart pointer to an array of objects (and not array of pointers).

5 Comments

When I do const std::unique_ptr<Position*[]> pos_pointer_array; My linter tells me there's a missing body for a lambda expression (error on the closing >)
@A.D I think your linter is broken.
@molbdnilo I'm using a freshly installed doom emacs (linter comes with doom), so while it is possible are you sure there couldn't be another explanation ? (I would compile to see what g++ has to say about it, but the codebase has other problems that prevent this...)
@A.D Is something preventing you from writing a declaration in a separate file and compiling it?
@molbdnilo Apart from my inability to somehow think to simply do it ? Nope, and I confirm you are right, time to fill an issue with the linter !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.