# StaticArrays and allocations

**URL:** <https://discourse.julialang.org/t/staticarrays-and-allocations/62123>\
**Category:** General Usage\
**Created:** [May 31, 2021, 10:02am UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123 "2021-05-31T10:02:57Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 31, 2021, 3:04pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/21 "2021-05-31T15:04:09Z")

</div>

> [@paulmelis](#):
>
> As you get more proficient with C/C++ you will know pretty well when to use stack-allocated versus heap-allocated values

But, I think the point made was that sometimes you want neither, you want the whole object to be optimized away. Is that also convenient to achieve in C/C++?

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [May 31, 2021, 3:11pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/22 "2021-05-31T15:11:21Z")

</div>

> [@DNF](#):
>
> But, I think the point made was that sometimes you want neither, you want the whole object to be optimized away. Is that also convenient to achieve in C/C++?

I think that’s only possible for stack-allocated objects in C++, where a compiler might optimize it that way. But heap-allocated ones are (probably) always present explicitly in memory. Plus, in C++ I don’t think I ever explicitly want “the whole object to be optimized away”. It’s simply not a choice I can really influence, so I don’t have to worry about it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 31, 2021, 3:18pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/23 "2021-05-31T15:18:07Z")

</div>

> [@paulmelis](#):
>
> Plus, in C++ I don’t think I ever explicitly want “the whole object to be optimized away”. It’s simply not a choice I can really influence, so I don’t have to worry about it.

I’m kinda not understanding how that’s a counter argument 🤔 You can’t have it, so that’s good?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 31, 2021, 3:22pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/24 "2021-05-31T15:22:54Z")

</div>

I may have missed a part there. You think it _could_ be available?

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [May 31, 2021, 3:24pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/25 "2021-05-31T15:24:17Z")

</div>

> [@DNF](#):
>
> I’m kinda not understanding how that’s a counter argument 🤔 You can’t have it, so that’s good?

Well, you made the argument that you wanted a certain optimization, which you can achieve in Julia, yet it comes at a cost of having to understand the how to achieve that. In C++ that optimization isn’t possible so wanting to achieve it doesn’t really apply. Perhaps with some black template magic you might be able to optimize objects away, but for 99.9% of the users I suspect that’s not really of interest to them (and only to those writing the templates, not the ones using them). The core C++ language (i.e. leaving out all the exotic stuff such as concepts, move semantics, funky template meta-programming, etc) is in many ways simpler than Julia due to the less sophisticated type system. So the choices you make as a developer appear to be simpler (but that’s again based on my very limited Julia experience).

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [May 31, 2021, 3:24pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/26 "2021-05-31T15:24:29Z")

</div>

> [@DNF](#):
>
> I may have missed a part there. You think it _could_ be available?

Sorry, what does this refer to?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 31, 2021, 3:28pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/27 "2021-05-31T15:28:00Z")

</div>

It refers to this sentence:

> [@paulmelis](#):
>
> I think that’s only possible for stack-allocated objects in C++, where a compiler might optimize it that way.

I missed that at first, and thought you said this could not happen in c++.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 31, 2021, 4:17pm UTC](https://discourse.julialang.org/t/staticarrays-and-allocations/62123/28 "2021-05-31T16:17:37Z")

</div>

> [@paulmelis](#):
>
> Case in point are my questions on the StaticArrays types as noted above, but looking at their docs once more I really think no guarantees are given, so the only option is to try them and see what comes out? Btw, all this certainly has to do with far less experience I have with Julia compared to C++.

I came from Fortran, and in Fortran (at least the one I knew) you don’t have any control over these things. Everything looks as if it was mutable, but the compilers will decide that without the user knowing about what is going on (and sometimes in a compiler-flag dependent way). Coming from Fortran, where every type is declared, one initially feels unsafe for not annotating types, then one makes mistakes and get type instabilities, and finally one gets the way around it and starts using Julia as one should. Not having to annotate every type becomes liberating at the end, not mentioning the fact that functions become generic, thus more useful. And we learn where to focus our attention to get performant code, and where all that obsessive control we had in the other language was counterproductive. At the end, not because of the possibilities of each language in general, but because the easiness of use, explore, test, benchmark, my codes are faster in Julia than what I achieved in Fortran.

[Previous page](https://discourse.julialang.org/t/staticarrays-and-allocations/62123.md?page=1)
