www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Initializing a fixed sized array

reply "finalpatch" <fengli gmail.com> writes:
This form is nice:

int[3] x = [1,2,3];

But it is horribly inefficient because it

1. allocates a dynamic array from the GC
2. writes 1,2,3 to the dynamic array
3. copies the 1,2,3 back to the static array

Or one can write:

int[3] x;
x[0] = 1;
x[1] = 2;
x[2] = 3;

That is a lot of typing, but also much more efficient than the 
first version. But if I understand correctly, each element still 
gets initialized to 0 before they got overwritten.

I would like to know whether we can have a way to initialize a 
fixed array in D like what we can do in C++.  In C++ if i write 
int x[3] = {1, 2, 3}; then it's done in the most optimal way, 
that is, simply assigning 1,2,3 to the 3 elements. no allocation, 
no zero fill, no excessive copy. Better, if we write const static 
int x[3] = {1,2,3}; then the array will be put in the read only 
segment of the executable and result in zero runtime overhead. 
However in D, static immutable int[3] x = [1,2,3]; still 
allocates and copies. I wonder can we have some sort of array 
initialization mechanism in D that is both nice and efficient?
May 31 2013
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, June 01, 2013 08:16:47 finalpatch wrote:
 This form is nice:
 
 int[3] x = [1,2,3];
 
 But it is horribly inefficient because it
 
 1. allocates a dynamic array from the GC
 2. writes 1,2,3 to the dynamic array
 3. copies the 1,2,3 back to the static array

Are you sure that that still allocates? I thought that that had been fixed. If it hasn't, it will be. The issue is completely temporary and not intended to be the case long term. Now, assuming that you're right and that still allocates, it's trivial to create a variadic function to initialize it for you by essentially generating int[3] x; x[0] = 1; x[1] = 2; x[2] = 3; for you. So, it's easy enough to get around, but the problem should either already be fixed in the compiler, or if not, it will be at some point in the near future. - Jonathan M Davis
May 31 2013
prev sibling next sibling parent "finalpatch" <fengli gmail.com> writes:
Oh cool, good to know this has been fixed. You are right, I just 
verified with DMD 2.063 and the generated code was good! Sorry 
about the noise.

On Saturday, 1 June 2013 at 06:28:45 UTC, Jonathan M Davis wrote:
 Are you sure that that still allocates? I thought that that had 
 been fixed. If
 it hasn't, it will be. The issue is completely temporary and 
 not intended to
 be the case long term.

 Now, assuming that you're right and that still allocates, it's 
 trivial to
 create a variadic function to initialize it for you by 
 essentially generating

 int[3] x;
 x[0] = 1;
 x[1] = 2;
 x[2] = 3;

 for you. So, it's easy enough to get around, but the problem 
 should either
 already be fixed in the compiler, or if not, it will be at some 
 point in the
 near future.

 - Jonathan M Davis

May 31 2013
prev sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Saturday, 1 June 2013 at 06:28:45 UTC, Jonathan M Davis wrote:
 On Saturday, June 01, 2013 08:16:47 finalpatch wrote:
 This form is nice:
 
 int[3] x = [1,2,3];
 
 But it is horribly inefficient because it
 
 1. allocates a dynamic array from the GC
 2. writes 1,2,3 to the dynamic array
 3. copies the 1,2,3 back to the static array

Are you sure that that still allocates? I thought that that had been fixed. If it hasn't, it will be. The issue is completely temporary and not intended to be the case long term. - Jonathan M Davis

Yes, it was a bug and it was fixed. Interesting that when there was this bug, many didn't know about it. After it was fixed, there were (and still are) comments warning about this issue.
Jun 01 2013