Change representation of dynamic arrays?

Janice Caron caron800 at googlemail.com
Fri Oct 19 23:31:36 PDT 2007


On 10/20/07, Walter Bright <newshound1 at digitalmars.com> wrote:
> 1) Passing dynamic arrays to printf as in:
>
>         printf("my string is %*.s\n", str);

This will break some of my old code, but none of my new code.

What you could do to fix that is to break it even more (ironically).
Make "printf" a reserved word, or an alias for writef, or /something/
which will cause any use of it to become a compile error.


> So, what do you think?

I had always assumed that, from the start, you designed it as { ptr,
length } deliberately in order to help the garbage collector.

If you change it, then you will be maintaining a pointer (the end
pointer) to something beyond the end of the array. For example,
suppose I do:

    int[] s = new int[4];
    int[] t = new int[1000];

now t.begin might end up being equal to s.end. But now when we do

    t = null;

or t goes out of scope or we otherwise assign t, suddenly that array
can no longer be freed, because the end pointer of s points to it.

(Not that I particularly care. Just pointing it out. Go for it).



More information about the Digitalmars-d mailing list