# Puzzle with memory allocation

**URL:** <https://discourse.julialang.org/t/puzzle-with-memory-allocation/29780>\
**Category:** General Usage\
**Created:** [October 11, 2019, 2:40pm UTC](https://discourse.julialang.org/t/puzzle-with-memory-allocation/29780 "2019-10-11T14:40:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 11, 2019, 2:40pm UTC](https://discourse.julialang.org/t/puzzle-with-memory-allocation/29780/1 "2019-10-11T14:40:00Z")

</div>

The line in the below code `if inrange(...` allocates for some reason a lot of memory. For the life of me I can’t figure out why. Any ideas?

```julia
        - # Initialize the output list
230953488 vlist= zeros(FInt, size(v,1)); nn= 0;
        - 
        - 
        - # Process the different options
    80448 if box != nothing
        0 sdim = size(v,2)
    46624 dim = Int(round(length(box)/2.))::FInt;
        0 @assert dim == sdim "Dimension of box not matched to dimension of array of vertices"
  1613833 abox = FFltVec(vec(box))
        0 inflatebox!(abox, inflatevalue)
        0 if sdim == 3
   660495 bx1, bx2, bx3, bx4, bx5, bx6 = FFltVec(abox)
        0 for j in 1:size(v,1)
2769886576 if inrange(bx1,bx2,v[j, 1]) &&
        - inrange(bx3,bx4,v[j, 2]) &&
        - inrange(bx5,bx6,v[j, 3])
        0 nn = nn + 1; vlist[nn] = j;
        - end
        - end
        - else

```

The function used above is defined as `@inline inrange(rangelo,rangehi,x) = (rangelo <= x <= rangehi)`. `v` is matrix of double precision numbers.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 11, 2019, 3:10pm UTC](https://discourse.julialang.org/t/puzzle-with-memory-allocation/29780/2 "2019-10-11T15:10:22Z")

</div>

> [@PetrKryslUCSD](#):
>
> bx1, bx2, bx3, bx4, bx5, bx6 = FFltVec(abox)

I wonder if this is the problem. does `FFltVec` return a Vector? If so, then the compiler may not be able to infer the types of bx1, … bx6 since the number of elements in FFltVec is not visible to the compiler.

Have you run `@code_warntype`?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 11, 2019, 3:14pm UTC](https://discourse.julialang.org/t/puzzle-with-memory-allocation/29780/3 "2019-10-11T15:14:42Z")

</div>

Nice. I was looking at it for too long, I think. Thanks!
