# Difference between Array ensureStorageAllocated and resize

**URL:** <https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703>\
**Category:** Development\
**Created:** [October 3, 2024, 12:24pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703 "2024-10-03T12:24:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 3, 2024, 12:24pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703/1 "2024-10-03T12:24:40Z")

</div>

I tried to debug both methods `Array::ensureStorageAllocated()` and `Array::resize()`.  
But actually it is too complicated for me, that’s why I would kindly ask if anyone could explain me what is actual difference?

As I understand `ensureStorageAllocated()` is useful when we want to avoid reallocating memory each time `Array::add()` is called. Because `ensureStorageAllocated()` reallocates all memory at once. But as I understand `resize()` does that at once also. Doesn’t it?

To be honest my main issue is preallocating memory for `juce::Path`. I see that `Path` has method `preallocateSpace()` and inside it there is used `Array::ensureStorageAllocated()`. But I am not sure if I use it in appropriate way which is:

I prepare and paint new `Path` in each callback of `VBlankAttachment`. What is worth to notice my `Path` in each callback has always the same num of points (to be more precise by point I mean `startNewSubpath()` followed by `lineTo()`).

In each callbeck before creating new `Path` I call `Path::clear()`. Due to fact `Path::clear()` does not rellocating memory (there is used `Array::clearQuick()`) I use `Path::preallocateSpace(4*numOfPoints + 8)` only once in my `Component::resized()`.

Is it appropraite implementation? Or should I call instead `Path::preallocateSpace(4*numOfPoints + 8)` in each `VBlankAttachment` callback?

Maybe it looks like the subject is not clearly related with `Array::ensureStorageAllocated()` but I just try to understand all those things.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 3, 2024, 12:42pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703/2 "2024-10-03T12:42:33Z")

</div>

Like a std::vector, juce::Array internally has a capacity (total number of allocated elements) and a size (number of elements in use), where the size is always smaller or equal to the capacity.

> [@pajczur](#):
>
> As I understand `ensureStorageAllocated()` is useful when we want to avoid reallocating memory each time `Array::add()` is called. Because `ensureStorageAllocated()` reallocates all memory at once. But as I understand `resize()` does that at once also. Doesn’t it?

Yes, if you call `ensureStorageAllocated (N)` or `resize (N)`, you’ll always have a capacity of at least N after the call. However, ensureStorageAllocated won’t affect the current size, whereas resize will set the current size to N.

> [@pajczur](#):
>
> Is it appropraite implementation? Or should I call instead `Path::preallocateSpace(4*numOfPoints + 8)` in each `VBlankAttachment` callback?

Assuming you’re reusing the same Path instance on each vblank callback, and that numOfPoints is constant, then you would only need to call this function once to reserve the required capacity.

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 3, 2024, 12:57pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703/3 "2024-10-03T12:57:43Z")

</div>

Great thanks for help

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 3, 2024, 1:09pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703/4 "2024-10-03T13:09:06Z")

</div>

But just to be perfectly sure.  
Let’s say for some reason I would create my own `MyPath` class (or something similar) and in `MyPath` I would provide two methods:

1. `MyPath::preallocateSpace()` - which would use internally (as it is in Juce) `Array::ensureStorageAllocated()`
2. `MyPath::resizeSpace()` - which would use internally `Array::resize()`

Then would it make a difference in my case if I call  
`MyPath::preallocateSpace(4*numOfPoints+8)`  
or  
`MyPath::resizeSpace(4*numOfPoints+8)`

Or it’s doesn’t matter?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 5, 2024, 4:57pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703/5 "2024-10-05T16:57:04Z")

</div>

> [@pajczur](#):
>
> Then would it make a difference in my case if I call  
> `MyPath::preallocateSpace(4*numOfPoints+8)`  
> or  
> `MyPath::resizeSpace(4*numOfPoints+8)`
> 
> Or it’s doesn’t matter?

In terms of allocations, it wouldn’t matter. You’d allocate the same amount of memory in each case.

The difference would be if you ever ended up using fewer than the expected number of points. If you called `resize(N)`, the path would have a size of N, so you’d need to make sure that all N points were valid. If you called `preallocateSpace(N)` instead, then you could more gracefully handle the situation where the path used fewer than N points.

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 9, 2024, 3:26pm UTC](https://forum.juce.com/t/difference-between-array-ensurestorageallocated-and-resize/63703/6 "2024-10-09T15:26:31Z")

</div>

Thank you very much, now it is clear for me.
