# Why is this code allocating?

**URL:** <https://discourse.julialang.org/t/why-is-this-code-allocating/127175>\
**Category:** Performance\
**Tags:** question, memory-allocation\
**Created:** [March 20, 2025, 2:41pm UTC](https://discourse.julialang.org/t/why-is-this-code-allocating/127175 "2025-03-20T14:41:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 20, 2025, 2:41pm UTC](https://discourse.julialang.org/t/why-is-this-code-allocating/127175/1 "2025-03-20T14:41:24Z")

</div>

Why is the following code allocating:

```julia
using VortexStepMethod

# Step 1: Define wing parameters
n_panels = 20 # Number of panels
span = 20.0 # Wing span [m]
chord = 1.0 # Chord length [m]
v_a = 20.0 # Magnitude of inflow velocity [m/s]
density = 1.225 # Air density [kg/m³]
alpha_deg = 30.0 # Angle of attack [degrees]
alpha = deg2rad(alpha_deg)

# Step 2: Create wing geometry with linear panel distribution
wing = Wing(n_panels, spanwise_panel_distribution=LINEAR)

# Add wing sections - defining only tip sections with inviscid airfoil model
add_section!(wing, 
    [0.0, span/2, 0.0], # Left tip LE 
    [chord, span/2, 0.0], # Left tip TE
    INVISCID)
add_section!(wing, 
    [0.0, -span/2, 0.0], # Right tip LE
    [chord, -span/2, 0.0], # Right tip TE
    INVISCID)

# Step 3: Initialize aerodynamics
body_aero::BodyAerodynamics = BodyAerodynamics([wing])

for panel in body_aero.panels end
@time for panel in body_aero.panels end

```

This gives me as output:

```julia
julia> @time for panel in body_aero.panels end
  0.000012 seconds (20 allocations: 640 bytes)

```

It is the second execution of the same line of code, which is `for panel in body_aero.panels end`. So I am not doing much, just iterating over all elements of a vector of Panel structs.

Iterating over a vector should not allocate, or what am I missing?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 20, 2025, 2:48pm UTC](https://discourse.julialang.org/t/why-is-this-code-allocating/127175/2 "2025-03-20T14:48:07Z")

</div>

OK, found the answer: I need to put my code into a function to measure allocations:

```julia
function test(body_aero)
    for panel in body_aero.panels end
end

test(body_aero)
@allocated test(body_aero)

```

This doesn’t allocate.
