digitalmars.D.learn - filling arrays, avoid default init
- Frank Benoit (keinfarbton) (8/8) Jan 09 2007 TypeA[] ta = .... ; // big array with something
- Lionello Lunesu (2/12) Jan 09 2007 type[] t = void;
- Lionello Lunesu (3/16) Jan 09 2007 Uhm, sorry. That doesn't seem to prevent the initialization at all...
- Thomas Kuehne (24/39) Jan 09 2007 -----BEGIN PGP SIGNED MESSAGE-----
- Lionello Lunesu (10/12) Jan 09 2007 Well, Frank's code:
- Thomas Kuehne (40/52) Jan 10 2007 -----BEGIN PGP SIGNED MESSAGE-----
- Frank Benoit (keinfarbton) (2/2) Jan 10 2007 Thanks for the answers.
- Stewart Gordon (17/44) Jan 11 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
Jan 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? -- Franktype[] t = void;
Jan 09 2007
Lionello Lunesu wrote:Frank Benoit (keinfarbton) wrote:Uhm, sorry. That doesn't seem to prevent the initialization at all... L.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? -- Franktype[] t = void;
Jan 09 2007
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Lionello Lunesu schrieb am 2007-01-09:Lionello Lunesu wrote: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-----Frank Benoit (keinfarbton) wrote:Uhm, sorry. That doesn't seem to prevent the initialization at all...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? -- Franktype[] t = void;
Jan 09 2007
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.Uhm, sorry. That doesn't seem to prevent the initialization at all...Can you provide a code sampel?
Jan 09 2007
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Lionello Lunesu schrieb am 2007-01-10: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-----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.Uhm, sorry. That doesn't seem to prevent the initialization at all...Can you provide a code sampel?
Jan 10 2007
Thanks for the answers. Frank
Jan 10 2007
Thomas Kuehne wrote:-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Lionello Lunesu schrieb am 2007-01-09:Actually, it's unpredictable, even dangerous.Lionello Lunesu wrote:Frank Benoit (keinfarbton) wrote:Uhm, sorry. That doesn't seem to prevent the initialization at all...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? -- Franktype[] t = void;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