# Is it safe to use juce::dsp::FixedSizeFunction?

**URL:** <https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237>\
**Category:** General JUCE discussion\
**Created:** [December 14, 2020, 11:54am UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237 "2020-12-14T11:54:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 14, 2020, 11:54am UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/1 "2020-12-14T11:54:17Z")

</div>

Found the class in the API documentation and it is exactly what I need for what I’m currently woking on. Then I was irritated that it could not be found until I realised that it obviously has not been added to the public interface yet.

So, is it safe to use it? Probably I would have written something similar (maybe not sooo sophisticated – great work!) myself, but I don’t see the point in doing that if such functionality is already available with JUCE. What’s the reason that it has not been added to the public headers until now?

---

<div class="post-metadata">

**Author:** ![FeRDNYC](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ferdnyc/32/7426_2.png) [@FeRDNYC](https://forum.juce.com/u/FeRDNYC)\
**Post date:** [December 16, 2020, 12:47am UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/2 "2020-12-16T00:47:21Z")

</div>

> [@PluginPenguin](#):
>
> What’s the reason that it has not been added to the public headers until now?

That template [didn’t exist](https://github.com/juce-framework/JUCE/commit/fca3429e6eb6cf4aecd3b1b7f86891547c0f2243#diff-413d31d917678b3c7ce1b13d90541178745265c86d1e78f5d91f4bad367184a4) until May of this year; it was only introduced to JUCE with version 6.0.0. But it was added to the public headers in the same commit that created it. So I don’t see any reason it wouldn’t be safe to use, assuming you can require a minimum version of JUCE \> 6.0.0.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 16, 2020, 8:44am UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/3 "2020-12-16T08:44:55Z")

</div>

> [@FeRDNYC](#):
>
> it was only introduced to JUCE with version 6.0.0. But it was added to the public headers in the same commit that created it.

Yes and no. The class itself was added with that commit but it has not been included in the module header (which is the usual JUCE way of making something available to the public API) until now, see here [https://github.com/juce-framework/JUCE/blob/b0c89174308a7eb2f6ad7c34617901ad4862d6e4/modules/juce\_dsp/juce\_dsp.h#L238](https://github.com/juce-framework/JUCE/blob/b0c89174308a7eb2f6ad7c34617901ad4862d6e4/modules/juce_dsp/juce_dsp.h#L238)

No FixedSizeFunction in there 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![FeRDNYC](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ferdnyc/32/7426_2.png) [@FeRDNYC](https://forum.juce.com/u/FeRDNYC)\
**Post date:** [December 16, 2020, 9:14am UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/4 "2020-12-16T09:14:49Z")

</div>

Ah, you’re right, my mistake. A `#include "containers/juce_FixedSizeFunction.h"` was added to the `juce_dsp.cpp` file on creation, but it wasn’t added to `juce_dsp.h` — and still isn’t there.

I agree, that’s confusing. 🤔

---

<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:** [December 16, 2020, 10:36am UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/5 "2020-12-16T10:36:15Z")

</div>

Thanks. Originally we’d hoped to use FixedSizeFunction in some places other than the Convolution, just to give it a bit more testing before locking-in the interface.

Unfortunately we haven’t found any more places in JUCE to use it (yet!) so I’ve gone ahead and made it public now:

> <https://github.com/juce-framework/JUCE/commit/e403e330efd9079b33b334f15f08f4a9f53aaca6>

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [December 16, 2020, 12:33pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/6 "2020-12-16T12:33:17Z")

</div>

The comment says “creating a FixedSizeFunction instance will never allocate”, i was wondering is this is right?

Shouldn’t it be “assigning a function/lambda to a FixedSizeFunction will never allocate”?

---

<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:** [December 16, 2020, 12:36pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/7 "2020-12-16T12:36:32Z")

</div>

The function’s buffer is stored directly inside the FixedSizeFunction (i.e. the `sizeof (FixedSizeFunction<400, ...>)` will be at least 400). I think this means that the current documentation is correct.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 16, 2020, 12:43pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/8 "2020-12-16T12:43:08Z")

</div>

I only see placement new being used with the functions internal storage buffer so I wonder how this one should ever allocate?

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [December 16, 2020, 1:06pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/9 "2020-12-16T13:06:21Z")

</div>

> [@reuk](#):
>
> The function’s buffer is stored directly inside the FixedSizeFunction

yes, but why does this change anything?

Maybe this is a language misunderstanding, but “creating a … instance” is for me the synonym for creating the object wether on stack or on the heap. And when the memory is inside the FixedSizeFunction, more closely std::aligned\_storage::type, it has to be allocated when creating the instance.

From the dsp::Convolution, i see that the queue which is holding the FixedSizeFunctions is created in advance. That makes sense.

can someone enlighten me, whats my thinking mistake ? 😉

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 16, 2020, 1:13pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/10 "2020-12-16T13:13:18Z")

</div>

I think this means that an instance of a FixedSizeFunction won’t perform any allocation itself.

If you take a `std::function`, it can be created on the stack but calling it’s constructor with certain arguments (or assigning something to it afterwards) will lead to an immediate memory allocation triggered by the instance. FixedSizeFunction will guarantee you to never do that.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [December 16, 2020, 1:18pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/11 "2020-12-16T13:18:28Z")

</div>

Thank you! I understand the working method exact like that.

But “FixedSizeFunction won’t perform any allocation itself.” is a big difference to “creating a FixedSizeFunction instance will never allocate”

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 16, 2020, 1:57pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/12 "2020-12-16T13:57:36Z")

</div>

So your point is that you could write e.g. `auto myFunction = std::make_unique<FixedSizeFunction<100, void(int)>>` and it would allocate heap memory?

In that case I think there is no single C++ type (besides maybe a completely empty struct) that could give you this guarantee – because if you decide to explicitly create an instance by allocating it on the heap it will always allocate. But creating a `FixedSizeFunction` as a stack variable – and I guess that is what it’s intended to be used like – won’t lead to heap allocation. Or do you mean something different?

Edit: re-read your post above and saw that you also mentioned stack memory allocation, sorry for overlooking that. But the I still come to the conclusion that this is basically the case for every C++ type, so talking about allocation in the context of a function in the namespace `dsp` makes it obvious for me that this is about heap memory allocation which is what most developers probably think of when you talk about allocation

---

<div class="post-metadata">

**Author:** ![kamedin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kamedin/32/11711_2.png) [@kamedin](https://forum.juce.com/u/kamedin)\
**Post date:** [December 16, 2020, 2:05pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/13 "2020-12-16T14:05:41Z")

</div>

> [@chkn](#):
>
> “creating a … instance” is for me the synonym for creating the object wether on stack or on the heap. And when the memory is inside the FixedSizeFunction, more closely std::aligned\_storage::type, it has to be allocated when creating the instance.

We only worry about heap allocations, because they lock.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [December 16, 2020, 2:18pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/14 "2020-12-16T14:18:23Z")

</div>

I only wanted to point out that the term “creating a FixedSizeFunction instance will never allocate” is completely miss-leading, especially for beginners. Maybe I’m looking at it too closely.

---

<div class="post-metadata">

**Author:** ![kamedin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kamedin/32/11711_2.png) [@kamedin](https://forum.juce.com/u/kamedin)\
**Post date:** [December 16, 2020, 2:31pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/15 "2020-12-16T14:31:57Z")

</div>

Well, strictly speaking you’re right but, as every object with size \> 0 is allocated somewhere, allocation always means heap allocation in the context where this is relevant. Still, it could be better phrased as “will never allocate on the heap”.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [December 16, 2020, 2:49pm UTC](https://forum.juce.com/t/is-it-safe-to-use-juce-fixedsizefunction/43237/16 "2020-12-16T14:49:43Z")

</div>

Thanks, when you learning something new, trying to understand, these linguistic inaccuracies can often make life difficult.
