# Bug? Feature? It's not expected behavior, that's for sure with Array::fill

**URL:** <https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164>\
**Category:** General JUCE discussion\
**Created:** [May 1, 2019, 3:41am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164 "2019-05-01T03:41:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 1, 2019, 3:41am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/1 "2019-05-01T03:41:34Z")

</div>

```auto
        array.ensureStorageAllocated(size);
        array.fill( {} );

```

You’d think that this would fill the array with whatever type it holds. But it doesn’t. that’s because `ensureStorageAllocated` doesn’t change `numUsed`, which is how `end()` is computed.

```auto
    void ensureStorageAllocated (int minNumElements)
    {
        const ScopedLockType lock (getLock());
        values.ensureAllocatedSize (minNumElements);
    }

```

```auto
//Array
    inline ElementType* end() const noexcept
    {
        return values.end();
    }

```

```auto
//ArrayBase
    inline ElementType* end() const noexcept
    {
        return elements + numUsed;
    }

```

which means that this loop for fill() is never entered:

```auto
    /** Fills the Array with the provided value. */
    void fill (const ParameterType& newValue) noexcept
    {
        const ScopedLockType lock (getLock());

        for (auto& e : *this)
            e = newValue;
    }

```

Any thoughts on why this is designed to not work as expected?

if `Array::fill` is designed to overwrite existing values, shouldn’t the name be changed?

this is the work-around for anyone who stumbled upon this:

```auto
        array.ensureStorageAllocated(size);
        for( int i = 0; i < size; ++i )
        {
            array.add( {} );
        }

```

or

```auto
array.insertMultiple(0, {}, size);

```

---

<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:** [May 1, 2019, 5:42am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/2 "2019-05-01T05:42:21Z")

</div>

> [@matkatmusic](#):
>
> if `Array::fill` is designed to overwrite existing values, shouldn’t the name be changed?

It’s designed as expected: fill() simply overwrite the array over `array.size()`  
you want to call `array.resize (numItems)` in your case ?  
`ensureStorageAllocated()` does not change the size (number of items) of the array, it only allocates the memory

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 1, 2019, 5:48am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/3 "2019-05-01T05:48:00Z")

</div>

not to be nit-picky, but `fill` implies that something is empty and will be filled up with something. `Array::fill` requires the array to already have a bunch of values in it before it can replace all of those values with the specified value.

`fill` is a bad name for what the function does.

I’m not sure how I missed `Array::resize` in the docs. thanks for suggesting that 🙂

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [May 1, 2019, 6:59am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/4 "2019-05-01T06:59:16Z")

</div>

All STL containers behave like this and it’s the correct, expected behavior.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 1, 2019, 7:19am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/5 "2019-05-01T07:19:29Z")

</div>

@Toddler-Boy Most of them have the `resize(num, valueToFillWith)` but only `std::array<>` has `fill` as an actual member function. and std::array\<\> allocates the space upon construction so std::array\<\>::fill() actually fills up the array with whatever value you tell it to fill up with.

Perhaps you’re thinking of std::fill( )? [https://en.cppreference.com/w/cpp/algorithm/fill](https://en.cppreference.com/w/cpp/algorithm/fill)  
std::fill() operates on valid iterators.  
juce::Array::fill operates on the array itself.

I think a documentation note that said

```auto
if the array has had nothing added to it (via .add() or .resize() ), 
this function will do nothing

```

would be helpful, and also that it

```auto
overwrites all existing values in the Array

```

It’s a subtle difference, but an important one and would have saved me (and probably others) a lot of debugging time if the documentation said those things.  
just my 2 cents…

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [May 1, 2019, 9:32am UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/6 "2019-05-01T09:32:27Z")

</div>

> [@matkatmusic](#):
>
> Perhaps you’re thinking of std::fill( )? [std::fill - cppreference.com](https://en.cppreference.com/w/cpp/algorithm/fill)  
> std::fill() operates on valid iterators.  
> juce::Array::fill operates on the array itself.

There’s no difference between a member function and a free function in this case, the key here is that `ensureStorageAllocated` is equivalent to calling `reserve` on a `std::vector`.

> [@matkatmusic](#):
>
> I think a documentation note that said
> 
> ```auto
> if the array has had nothing added to it (via .add() or .resize() ), 
> this function will do nothing
> 
> ```
> 
> would be helpful, and also that it
> 
> ```auto
> overwrites all existing values in the Array
> 
> ```
> 
> It’s a subtle difference, but an important one and would have saved me (and probably others) a lot of debugging time if the documentation said those things.  
> just my 2 cents…

It’s a misunderstanding of the `ensureStorageAllocated` function not the `fill` function surly you expected `fill` to do nothing if the array was empty? maybe `ensureStorageAllocated` should be clearer in stating that is does not change the size of the array? It could also have a link to the `resize` function.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 1, 2019, 5:04pm UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/7 "2019-05-01T17:04:42Z")

</div>

> [@anthony-nicholls](#):
>
> surly you expected `fill` to do nothing if the array was empty

no, I expected, after i allocated storage, to fill that storage with values via the `fill` function.

I don’t think that’s an _unreasonable_ expectation, given the names of the functions…

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [May 1, 2019, 5:50pm UTC](https://forum.juce.com/t/bug-feature-its-not-expected-behavior-thats-for-sure-with-array-fill/33164/8 "2019-05-01T17:50:45Z")

</div>

Exactly you expected the array _wasn’t_ empty after calling `ensureStorageAllocated` so the documentation for `ensureStorageAllocated` is surely what needs addressing _not_ `fill`, although there’s no harm in updating both of course.

I don’t think it’s completely unreasonable but I think it’s equivalent to the following…

```auto
array.reserve(size);
std::fill(array.begin(), array.end(), {});

```
