# Array\<Value\> question

**URL:** <https://forum.juce.com/t/array-value-question/14184>\
**Category:** General JUCE discussion\
**Created:** [January 18, 2015, 11:00am UTC](https://forum.juce.com/t/array-value-question/14184 "2015-01-18T11:00:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![soje](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@soje](https://forum.juce.com/u/soje)\
**Post date:** [January 18, 2015, 11:00am UTC](https://forum.juce.com/t/array-value-question/14184/1 "2015-01-18T11:00:25Z")

</div>

I have a set of sliders (24), that each should refer to a Value. The Value objects are declared and updated from midi in another class.

This may be a silly newbie question, but can I make an Array to hold all the Value objects?

&nbsp;

Thankyou for your patience!

Søren

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [January 18, 2015, 11:31am UTC](https://forum.juce.com/t/array-value-question/14184/2 "2015-01-18T11:31:00Z")

</div>

The answer&nbsp;is yes -&nbsp;it should be fine. &nbsp;But it's actually a very important question. &nbsp;Different arrays have different types of behaviour, and Value has a slightly odd behaviour when you copy it as well (it forgets about any listeners).&nbsp;

The array behaviour comes from how it deals with expansion once you've filled up the space it had allocated. &nbsp;Array and std::vector (the standard C++ alternative)&nbsp;both hold the memory in one continuous block. &nbsp;If you add more objects than they have space reserved for they grab a new bigger contiguous block and move everything across. &nbsp;

- **juce::Array\<\>** &nbsp;calls memcpy and just copies the data from one location to another. That's fine, except if your object has a pointer back to itself (or otherwise depends on being in a fixed location in memory). &nbsp;So it's okay for most objects but won't work with, say, a std::function\<\> object. &nbsp;Array\<std::function\<\>\> breaks, at least on the C++ library I'm&nbsp;using.&nbsp;
- **std::vector\<\>** is slightly better behaved with most objects. &nbsp;It uses the move or copy constructors when it needs to expand. &nbsp;However, because Value doesn't copy the listeners when its copy constructor is called it doesn't work well with a Value object.&nbsp;

The really safe&nbsp;alternative for complicated objects, or anywhere you need polymorphic pointers, is a **juce::OwnedArray\*.&nbsp;** But vector and array will often be faster, which may be important in some circumstances. &nbsp;And they are a bit easier to use as you don't need to bother allocating everything separately with&nbsp; **new**.&nbsp;

So it's not a silly question at all. &nbsp;There's probably much more to it, but these are the issues I've come across..

**\*** Jules - or an array/or vector&nbsp;of ScopedPointers? is there any difference?

&nbsp;

---

<div class="post-metadata">

**Author:** ![soje](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@soje](https://forum.juce.com/u/soje)\
**Post date:** [January 18, 2015, 9:15pm UTC](https://forum.juce.com/t/array-value-question/14184/3 "2015-01-18T21:15:00Z")

</div>

Thankyou for your answer!

&nbsp;

So if I use juce::Array, "ensureStorageAllocated"&nbsp;should make shure no copying is done and the Values don't forget the listeners?

&nbsp;

Regards

Søren

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [January 18, 2015, 11:15pm UTC](https://forum.juce.com/t/array-value-question/14184/4 "2015-01-18T23:15:36Z")

</div>

Array won't anyway. &nbsp;It's std::vector that's the problem with Value listeners.
