Thread overview
filling arrays, avoid default init
Jan 09, 2007
Lionello Lunesu
Jan 09, 2007
Lionello Lunesu
Jan 09, 2007
Thomas Kuehne
Jan 10, 2007
Lionello Lunesu
Jan 10, 2007
Thomas Kuehne
Jan 11, 2007
Stewart Gordon
January 09, 2007
TypeA[] ta = .... ; // big array with something
TypeB[] tb;
tb.length = ta.length; // (1)
foreach( uint i, TypeA a; ta ){
  tb[i] = ta[i].getB();
}

(1) how can I avoid the default initialization?

-- Frank
January 09, 2007
Frank Benoit (keinfarbton) wrote:
> TypeA[] ta = .... ; // big array with something
> TypeB[] tb;
> tb.length = ta.length; // (1)
> foreach( uint i, TypeA a; ta ){
>   tb[i] = ta[i].getB();
> }
> 
> (1) how can I avoid the default initialization?
> 
> -- Frank

type[] t = void;
January 09, 2007
Lionello Lunesu wrote:
> Frank Benoit (keinfarbton) wrote:
>> TypeA[] ta = .... ; // big array with something
>> TypeB[] tb;
>> tb.length = ta.length; // (1)
>> foreach( uint i, TypeA a; ta ){
>>   tb[i] = ta[i].getB();
>> }
>>
>> (1) how can I avoid the default initialization?
>>
>> -- Frank
> 
> type[] t = void;

Uhm, sorry. That doesn't seem to prevent the initialization at all...

L.
January 09, 2007
Lionello Lunesu schrieb am 2007-01-09:
> Lionello Lunesu wrote:
>> Frank Benoit (keinfarbton) wrote:
>>> TypeA[] ta = .... ; // big array with something
>>> TypeB[] tb;
>>> tb.length = ta.length; // (1)
>>> foreach( uint i, TypeA a; ta ){
>>>   tb[i] = ta[i].getB();
>>> }
>>>
>>> (1) how can I avoid the default initialization?
>>>
>>> -- Frank
>> 
>> type[] t = void;
>
> Uhm, sorry. That doesn't seem to prevent the initialization at all...

Can you provide a code sampel?

Tested on Linux:
# import std.stdio;
#
# typedef int X = 12_34_56_78;
#
# void main(){
#    X[4] a;
#    X[4] b = void;
#
#    writefln("a: %s", a);
#    writefln("b: %s", b);
# }

a: [12345678,12345678,12345678,12345678]
b: [-607311696,-609996928,0,-608522976]

Thomas


January 10, 2007
>> 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.

L.


January 10, 2007
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


January 10, 2007
Thanks for the answers.
Frank
January 11, 2007
Thomas Kuehne wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Lionello Lunesu schrieb am 2007-01-09:
>> Lionello Lunesu wrote:
>>> Frank Benoit (keinfarbton) wrote:
>>>> TypeA[] ta = .... ; // big array with something
>>>> TypeB[] tb;
>>>> tb.length = ta.length; // (1)
>>>> foreach( uint i, TypeA a; ta ){
>>>>   tb[i] = ta[i].getB();
>>>> }
>>>>
>>>> (1) how can I avoid the default initialization?
>>>>
>>>> -- Frank
>>> type[] t = void;
>> Uhm, sorry. That doesn't seem to prevent the initialization at all...

Actually, it's unpredictable, even dangerous.

> Can you provide a code sampel?

----------
import std.stdio;

void main() {
    int[] data = void;

    writefln("%08x %08x", data.ptr, data.length);
    data.length = 16;
    writefln("%08x %08x", data.ptr, data.length);
    writefln(data);
}
----------

You'll be lucky if the last statement doesn't throw an AV.  The danger arises if you try to write to the memory at the uninitialised pointer.

> Tested on Linux:
> # import std.stdio;
> # # typedef int X = 12_34_56_78;
> # # void main(){
> #    X[4] a;
> #    X[4] b = void;
<snip>

Of course that works.  That's a static array.

Stewart.