Re: double use of indices in SPH density loop

From: Volker Springel <vspringel_at_MPA-Garching.MPG.DE>
Date: Tue, 25 May 2021 15:52:34 +0200

Dear Leonard,

Thanks for pointing this out, but this is fine in C++ because of the declaration of the loop variables inside the for-statements themselves. This gives these variables local scope in the loops, and in particular, you can nest loops with the same loop variable just fine. The inner loop's variable doesn't influence that of the outer variable at all, however, it shadows the outer loop variable, i.e. its value can then not be accessed in the inner loop. Having said this, it is certainly advisable to avoid such name collisions in non-trivial loops as a proactive measure against accidental coding errors and for readibility.

Best,
Volker

> On 25. May 2021, at 10:58, Leonard Romano <leonard.romano_at_tum.de> wrote:
>
> Dear gadget-list members,
>
> I just noticed that during the computation of the SPH density, there is a possibly dangerous double use of loop indices.
> First note that the loop over the workstack for doing final operations on the results uses the index "i":
>
> for(int i = 0; i < ndensities; i++)
>
> later in the same loop when hfac is computed using a loop, it is indexed with the same "i":
>
> for(int i = 0; i < NUMDIMS; i++)
>
> This could potentially lead to problems and should be changed to something else (like "j").
>
> Best,
> Leonard
>
> --
> ===================================================
> Leonard Romano, B.Sc.(レオナルド・ロマノ)
> Physics Department
> Technical University of Munich (TUM), Germany
> Theoretical Astrophysics Group
> Department of Earth and Space Science
> Graduate School of Science, Osaka University, Japan
> he / him / his
> ===================================================
>
>
> -----------------------------------------------------------
>
> If you wish to unsubscribe from this mailing, send mail to
> minimalist_at_MPA-Garching.MPG.de with a subject of: unsubscribe gadget-list
> A web-archive of this mailing list is available here:
> http://www.mpa-garching.mpg.de/gadget/gadget-list
Received on 2021-05-25 15:52:35

This archive was generated by hypermail 2.3.0 : 2023-01-10 10:01:32 CET