OwnedArray replacement in modern C++ std

I wonder if it is possible to hold dynamically with std::unique_ptr or new(...) created objects in an array in C++ without the need to do manual memory management. Like with juce::OwnedArray.

It’s a question that comes to my mind from time to time and I wasn’t able to find a solution so far. My goal is to use as many standard C++ features from the std namespace as possible, especially in the DSP-related code.

Or is it common to avoid this situation and only use objects with a default constructor that I can put into arrays on the stack and add some init functions that take the dependent objects?

Any input is welcome.


(it’s used in different places in the juce codebase if you want to look for examples)

1 Like

Thanks for the fast answer and for the hint about the JUCE codebase. I’m surprised that it is so simple and that this is already memory leak safe. I will use that one from now on.

AFAIK any of the STL smart pointers are safe to use in any of the STL containers?

Why not use OwnedArray though? It has a much more convenient interface IMO.

1 Like

In a lot of cases, I do not manipulate the list itself while the program is running. I only need access to the entries with an index. Because of this, I don’t need most of the OwnedArray features.
I prefer standard language features if possible because of portability and also for educational reasons.