digitalmars.D.learn - Shortest way to allocate an array and initialize it with a specific
- Adel Mamin (5/5) Jun 10 2015 ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes.
- Low Functioning (2/7) Jun 10 2015 Assign void, then assign whatever?
- Michael Coulombe (3/8) Jun 10 2015 Probably:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (12/17) Jun 10 2015 Another option:
- "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> (5/10) Jun 11 2015 Is
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (4/15) Jun 11 2015 Yes, it uses `uninitializedArray()` if the length is known (which
- weaselcat (3/10) Jun 11 2015 just an fyi, gdc optimizes this away(looks like it overwrites the
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (12/17) Jun 11 2015 On Wed, 10 Jun 2015 20:22:17 +0000
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (2/20) Jun 11 2015 I like this one :-)
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (4/29) Jun 11 2015 small enhancment:
- Steven Schveighoffer (5/34) Jun 11 2015 import std.typecons;
ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?
Jun 10 2015
On Wednesday, 10 June 2015 at 20:22:18 UTC, Adel Mamin wrote:ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?Assign void, then assign whatever?
Jun 10 2015
On Wednesday, 10 June 2015 at 20:22:18 UTC, Adel Mamin wrote:ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?Probably: auto a2 = value.repeat(size).array;
Jun 10 2015
On 06/10/2015 01:22 PM, Adel Mamin wrote:ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?Another option: void main() { auto a2 = new ubyte[5]; a2[] = 0xAA; // <-- Assign to "all elements" /* Alternative syntax: * * auto a2 = new ubyte[](5); */ } Ali
Jun 10 2015
On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:Another option: void main() { auto a2 = new ubyte[5];But this causes an extra zero-initialization of a2.a2[] = 0xAA; // <-- Assign to "all elements"Is auto a2 = value.repeat(size).array; better in this regard?
Jun 11 2015
On Thursday, 11 June 2015 at 07:57:47 UTC, Per Nordlöw wrote:On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:Yes, it uses `uninitializedArray()` if the length is known (which it is for `repeat()`): https://github.com/D-Programming-Language/phobos/blob/master/std/array.d#L111Another option: void main() { auto a2 = new ubyte[5];But this causes an extra zero-initialization of a2.a2[] = 0xAA; // <-- Assign to "all elements"Is auto a2 = value.repeat(size).array; better in this regard?
Jun 11 2015
On Thursday, 11 June 2015 at 07:57:47 UTC, Per Nordlöw wrote:On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:just an fyi, gdc optimizes this away(looks like it overwrites the typeinfo,) ldc does not.Another option: void main() { auto a2 = new ubyte[5];But this causes an extra zero-initialization of a2.
Jun 11 2015
On Wed, 10 Jun 2015 20:22:17 +0000 Adel Mamin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?import std.stdio; struct Ubyte(ubyte defval) { ubyte v = defval; alias v this; } void main() { auto a2 = new Ubyte!(0xAA)[5]; writeln(a2); }
Jun 11 2015
On Thursday, 11 June 2015 at 08:33:46 UTC, Daniel Kozák wrote:On Wed, 10 Jun 2015 20:22:17 +0000 Adel Mamin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I like this one :-)ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?import std.stdio; struct Ubyte(ubyte defval) { ubyte v = defval; alias v this; } void main() { auto a2 = new Ubyte!(0xAA)[5]; writeln(a2); }
Jun 11 2015
On Thu, 11 Jun 2015 11:43:25 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Thursday, 11 June 2015 at 08:33:46 UTC, Daniel Kozák wrote:small enhancment: struct Ubyte(ubyte defval = 0)On Wed, 10 Jun 2015 20:22:17 +0000 Adel Mamin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I like this one :-)ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?import std.stdio; struct Ubyte(ubyte defval) { ubyte v = defval; alias v this; } void main() { auto a2 = new Ubyte!(0xAA)[5]; writeln(a2); }
Jun 11 2015
On 6/11/15 7:51 AM, Daniel Kozák via Digitalmars-d-learn wrote:On Thu, 11 Jun 2015 11:43:25 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:import std.typecons; alias Ubyte(ubyte defval = 0) = Typedef!(ubyte, defval); I personally like the range solution the best, it has the most flexibility. -SteveOn Thursday, 11 June 2015 at 08:33:46 UTC, Daniel Kozák wrote:small enhancment: struct Ubyte(ubyte defval = 0)On Wed, 10 Jun 2015 20:22:17 +0000 Adel Mamin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I like this one :-)ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?import std.stdio; struct Ubyte(ubyte defval) { ubyte v = defval; alias v this; } void main() { auto a2 = new Ubyte!(0xAA)[5]; writeln(a2); }
Jun 11 2015