Change representation of dynamic arrays?

Walter Bright newshound1 at digitalmars.com
Sat Oct 20 00:27:09 PDT 2007


Janice Caron wrote:
> 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.

Just add 'deprecate' <g>.

> 
> 
>> 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.

No prob, t=null already sets both members to 0.

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



More information about the Digitalmars-d mailing list