string initialization question.

Steven Schveighoffer schveiguy at yahoo.com
Fri Jul 30 13:10:46 PDT 2010


On Fri, 30 Jul 2010 15:56:36 -0400, Jonathan M Davis  
<jmdavisprog at gmail.com> wrote:

> On Friday, July 30, 2010 10:14:45 Steven Schveighoffer wrote:
>> I think a function to do it is fine, like makeArray('-', 5);
>
> Well, creating a function for producing an array literal and returning  
> it using
> templates and string mixins wouldn't be all that hard, but if you want  
> to create
> a dynamic array of arbitrary size at runtime, that's not going to work,  
> and a
> makeArray() function would have exactly the same tools that you have to  
> create
> an array of all the same value.

The function would call gc_malloc directly, which does not initialize the  
requested memory.  Actually, it would call a new run time function that I  
will write, which would initialize the array length also.

> So, it's not going to be any more efficient that
> what you can do.
>
> int[] a = new int[](x);
> a[] = val;
>
> _should_ be fairly easily optimized by the compiler and thus really  
> should be
> optimized down to an initialization rather than an initialization and an
> assignment.

It's not.  The only runtime functions available to the compiler look like  
this:

_d_newarrayT(TypeInfo ti, size_t length);

-Steve


More information about the Digitalmars-d-learn mailing list