www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - filling arrays, avoid default init

reply "Frank Benoit (keinfarbton)" <benoit tionex.removethispart.de> writes:
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
Jan 09 2007
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
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;
Jan 09 2007
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
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.
Jan 09 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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...
Can you provide a code sampel? Tested on Linux: a: [12345678,12345678,12345678,12345678] b: [-607311696,-609996928,0,-608522976] Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFpCBCLK5blCcjpWoRAqMaAJ99ABE9fuCCRBZ65VbRF40BPYNsZwCfblY2 DDkPgs/4g0YjXFRyQUSy+sA= =w9m/ -----END PGP SIGNATURE-----
Jan 09 2007
next sibling parent reply "Lionello Lunesu" <lionello lunesu.remove.com> writes:
 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.
Jan 09 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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.) .. elements]; Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFpXAkLK5blCcjpWoRAqY0AJ91qv+e5pUbdpiQD+iFnt00kys8wwCfaeHc 5wjtvN4giWqxREMyt5kgzC0= =F6Tm -----END PGP SIGNATURE-----
Jan 10 2007
parent "Frank Benoit (keinfarbton)" <benoit tionex.removethispart.de> writes:
Thanks for the answers.
Frank
Jan 10 2007
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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:







<snip> Of course that works. That's a static array. Stewart.
Jan 11 2007