# Memory allocation when using broadcasting macro @. with long expression?

**URL:** <https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238>\
**Category:** General Usage\
**Tags:** macros, broadcast\
**Created:** [October 12, 2020, 4:36pm UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238 "2020-10-12T16:36:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [October 12, 2020, 4:36pm UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/1 "2020-10-12T16:36:12Z")

</div>

It seems that if the expression is a bit long then `@.` allocates memory whereas manually using `.` does not. Why?  
Here is an expression. No memory allocation if NOT including the last term within the brackets, but allocates if it is included.  
` @. f_Fe = 1.0/(1.0 + KFeCl*ClBW + KFeSO4*THSO4*f_SO4 + KFeCO3*TCO2*f_CO3 + KFeCO3OH*TCO2*f_CO3*KW/H + KFeHS*TH2S*f_HS + KFeS*TH2S*f_HS/H)`  
This does not allocate  
` f_Fe .= 1.0./(1.0 .+ KFeCl.*ClBW .+ KFeSO4.*THSO4.*f_SO4 .+ KFeCO3.*TCO2.*f_CO3 .+ KFeCO3OH.*TCO2.*f_CO3.*KW./H + KFeHS.*TH2S.*f_HS .+ KFeS.*TH2S.*f_HS./H)`  
Aren’t they supposed to be identical?  
So is there a limit of the length of the expression?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 12, 2020, 4:41pm UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/2 "2020-10-12T16:41:48Z")

</div>

The problem you’re running into is that Julia currently treats + as an n-ary operation, but gets mad when it is called with too many arguments. The short term fix is to add parentheses to break things up slightly.

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [October 13, 2020, 11:04am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/3 "2020-10-13T11:04:20Z")

</div>

That works but it’s tricky to figure out where to put the brackets!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 13, 2020, 11:10am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/4 "2020-10-13T11:10:49Z")

</div>

You can do

```julia
const ⊕ = +

```

and just use that instead of `+`…

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [October 13, 2020, 11:12am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/5 "2020-10-13T11:12:55Z")

</div>

what’s the theory behind that?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 13, 2020, 11:16am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/6 "2020-10-13T11:16:51Z")

</div>

Then it no longer parses a n-ary.

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [October 13, 2020, 12:16pm UTC](https://discourse.julialang.org/t/memory-allocation-when-using-broadcasting-macro-with-long-expression/48238/7 "2020-10-13T12:16:45Z")

</div>

Cool. Thanks!
