Memory leak with dynamic array

Joseph Wakeling joseph.wakeling at webdrake.net
Mon Apr 12 10:35:54 PDT 2010


Steven Schveighoffer wrote:
> On Mon, 12 Apr 2010 12:03:38 -0400, Joseph Wakeling
> <joseph.wakeling at gmail.com> wrote:
> 
>> I thought dev effort was now focusing back on GDC ... ? :-P
> 
> AFAIK, gdc hasn't been actively developed for a few years.
> 
> ldc, on the other hand, has regular releases.  I think ldc may be the
> future of D compilers, but I currently use dmd since I'm using D2.

http://bitbucket.org/goshawk/gdc/wiki/Home

:-)

Either way I'm happy.  I don't have any issues with dmd, but I do want
to see a properly free D compiler that can be prepackaged in all the
Linux distros.

> Yes, you get around this by preallocating.

Sure.  But that wasn't what shocked me -- what I was amazed by was
setting up a situation where I _had_ preallocated the memory and still
seeing the memory usage explode, because D was preserving the memory
from each round of the loop.

(Actually I'm still having some issues with this, despite using
assumeSafeAppend, but more on that in a separate email.)

> It's often these types of performance discrepancies that critics point
> to (not that you are a critic), but it's the cost of having a more
> comprehensive language.  Your appetite for the sheer performance of a
> language will sour once you get bit by a few of these nasty bugs.

For sure.

> But D fosters a completely different way of thinking about solving
> problems.

I can see how the example you give would be fantastically useful.

More generally, I think this is the point -- I need to adjust my head to
writing D-ish code, just as when moving from C to C++ I needed to switch
to various new ways of doing things.

> There are many in the community that use D for numerical stuff.  It's
> definitely not as mature as it could be, but getting better.  Don is
> adding a lot of cool stuff to it, including a builtin exponent operator
> and arbitrary precision numbers.

I guessed there would be -- I knew for example that there was someone
out there working on a fairly major mathematical/numerical library for
D, but it's a while since I checked that out.

So take my earlier comment about numerical work to refer only to doing
things the way I'm used to ... ;-)

> Yes, but that's not what I meant ;)  I mean, you can write your own
> types, like the Appender (or what the appender *should* be) that
> optimize the behavior of code to meet any needs.  And it can do it with
> a much better syntax than C.  D's template system and ability to make
> user-types seem like builtins I think is unparalleled in C-like languages.

Hence much pleasure and excitement in learning D ... :-)

Don wrote:
> There are quite a lot of us here with exactly that kind of background.
> 
> Something about the array issue -- D dynamic arrays are heavily geared towards algorithms which perform an initial allocation and afterwards avoid memory allocation entirely.
> In D, such slicing algorithms are extremely clean, extremely fast, and memory safe.
> In C++, it's much more difficult to write code in that manner. A straightforward translation from C++ will generally miss the benefits of D arrays, and you'll end up with slower code.

Exactly my current situation. :-P

> A kind of "Zen of D" is to use array slices as much as possible.

I will look into this more and see if this approach can help with some
of my code -- are there existing projects I could take a look at to get
some examples?

Thanks & best wishes,

    -- Joe


More information about the Digitalmars-d-learn mailing list