Just another question about memory management in d from a newbie

Adam D. Ruppe destructionator at gmail.com
Mon Jun 17 20:00:34 UTC 2019


On Monday, 17 June 2019 at 19:53:52 UTC, Thomas wrote:
> Do I understand it right that the variable x will be created 
> 10000 times and destroyed at the end of the scope in each loop 
> ? Or will it be 10000 overwritten by creation ?

No, the compiler will generate code to reuse the same thing each 
loop.

> In my opinion this version should perform a little better:

That's actually slightly *less* efficient (in some situations) 
because it doesn't allow the compiler to reuse the memory for `x` 
after the loop has exited.


But in both cases, the compiler will just set aside a little bit 
of local space (or just a cpu scratch register) and use it 
repeatedly. It is totally "free".


More information about the Digitalmars-d-learn mailing list