# OwnedArray replacement in modern C++ std

**URL:** <https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032>\
**Category:** Development\
**Created:** [December 6, 2022, 3:44pm UTC](https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032 "2022-12-06T15:44:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [December 6, 2022, 3:44pm UTC](https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032/1 "2022-12-06T15:44:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [December 6, 2022, 3:51pm UTC](https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032/2 "2022-12-06T15:51:09Z")

</div>

`std::vector<std::unique_ptr<>>`

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

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [December 6, 2022, 4:01pm UTC](https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032/3 "2022-12-06T16:01:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [December 6, 2022, 4:23pm UTC](https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032/4 "2022-12-06T16:23:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [December 6, 2022, 4:38pm UTC](https://forum.juce.com/t/ownedarray-replacement-in-modern-c-std/54032/5 "2022-12-06T16:38:50Z")

</div>

> [@ImJimmi](#):
>
> Why not use OwnedArray though? It has a much more convenient interface IMO.

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.
