A program leaking memory.

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon Mar 13 23:32:53 PST 2006


Johan Grönqvist wrote:
> 1) Static vs. Dynamic arrays:
> --------------------------------------------------------
> The system size I use will not be known at compile time, and my
> understanding is that I can therefore not use
> 
> real[myLength] grad;
> 
> but have to use
> 
> real[] grad;
> grad.length = myLength;
> 
> where myLength is computed earlier.
> 
> The arrays I initialize in this way are never again resized. With this I
> mean that the two first things I do are
> 
> real[] grad;
> grad.lenth = myLength;
> 
> thereafter I modify the elements and read off their values a lot, but I
> never resize the array again, i.e., the memory need for grad should
> never change during a step, and as soon as one step ends and the next
> starts the old grad-vector is no loner needed.

Since you never resize the array, it should be perfectly feasible to use `real[myLength] 
grad;` instead.  You might also try using 'foreach' but that doesn't solve your problem, 
just makes it more readable (IMHO).  One thing you might try is:

# real[] grad = new real[myLength];
# // ... loop ...
# delete grad;

I'm pretty sure you can use 'delete' with anything 'new'd, as in C++.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list