filling arrays, avoid default init
Thomas Kuehne
thomas-dloop at kuehne.cn
Wed Jan 10 14:07:17 PST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lionello Lunesu schrieb am 2007-01-10:
>>> Uhm, sorry. That doesn't seem to prevent the initialization at all...
>>
>> Can you provide a code sampel?
>
> Well, Frank's code:
>
> TypeA[] ta = .... ; // big array with something
> TypeB[] tb = void;
> tb.length = ta.length; // (1)
> foreach( uint i, TypeA a; ta ){
> tb[i] = ta[i].getB();
> }
>
> For unsized dynamic arrays (TypeB[]), =void does not prevent the
> initialization when you change the length later.
It is working as advertised in
http://www.digitalmars.com/d/declaration.html (Void Initializations)
http://www.digitalamrs.com/d/arrays.html (Setting Dynamic Array Length)
The below code might help solve your problem with new'ing dynamic
arrays. (Windows most likely requires some "import" changes.)
# import internal.gc.gclinux;
# import internal.gc.gcbits;
# import internal.gc.gcx;
# import std.gc;
#
# /**
# * Void Initialization of a dynamic GC collected array
# *
# * Only use this template if you know what you are doing.
# */
# template newVoidArray(T){
# T[] newVoidArray(size_t elements){
# return (cast(T*) (cast(GC)getGCHandle()).malloc(elements * T.sizeof))[0 .. elements];
# }
# }
#
# import std.stdio;
# typedef int X = 12_34_56_78;
#
# void main(){
# X[] a = new X[4];
# X[] b = newVoidArray!(X)(4);
#
# writefln("a: %s", a);
# writefln("b: %s", b);
# }
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFFpXAkLK5blCcjpWoRAqY0AJ91qv+e5pUbdpiQD+iFnt00kys8wwCfaeHc
5wjtvN4giWqxREMyt5kgzC0=
=F6Tm
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-learn
mailing list