www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dynamic array initialization syntax

reply bearophile <bearophileHUGS lycos.com> writes:
I have suggested a simple initialization syntax for dynamic arrays, similar to
the syntax used for fixed-sized arrays:
http://d.puremagic.com/issues/show_bug.cgi?id=5603

void main() {
    auto a2 = new int[5] = void;
    auto a1 = new int[5] = 1;
    auto m2 = new int[][](5, 5) = void;
    auto m1 = new int[][](5, 5) = 1;
}

Surely I am not the first person to suggest this :-)

Bye,
bearophile
Feb 16 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
import std.stdio;
import std.range;

auto newArray(T)(T value, size_t size)
{
    return array(take(repeat(value), size));
}

void main()
{
    auto a1 =3D newArray(5, 3);
    assert(a1 =3D=3D [5, 5, 5]);
}


__Dmain:; Function begin, communal
        sub     esp, 28                                 ; 0000 _ 83. EC, 1C
        mov     dword [esp+8H], 5                       ; 0003 _ C7.
44 24, 08, 00000005
        mov     dword [esp+4H], 1                       ; 000B _ C7.
44 24, 04, 00000001
        mov     edx, dword [esp+8H]                     ; 0013 _ 8B. 54 24,=
 08
        mov     eax, dword [esp+4H]                     ; 0017 _ 8B. 44 24,=
 04
        push    edx                                     ; 001B _ 52
        push    eax                                     ; 001C _ 50
        call
_D3std5array72__T=06=CC=94S=05=D8=B2ange43=03=D7=B4Take=0C=D6=B1=04=D6=B6Re=
peatTiZ=07=CB=9A=05=C9=9A=06=C6=86>=C6=81i; 001D _ E8,
00000000(rel)
        add     esp, 28                                 ; 0022 _ 83. C4, 1C
        xor     eax, eax                                ; 0025 _ 31. C0
        ret                                             ; 0027 _ C3
; __Dmain End of function

Is that good? I can't tell, I'm still working my way around the asm book. :=
)

But you did say there's an extra __memset32 call in your #2 example in
the bug issue report, and I don't see one here.
Feb 16 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Oh, but there's a call to array. I guess that could slow things down, sorry.
Feb 16 2011