April 10, 2007
Frits van Bommel Wrote:

> Dan wrote:
> > Graham Wrote:
> >> (And to be pedantic, int[5][5] is a 2D rectangular array, which in memory is exactly the same as int[25] - a 1D array, of course - so six of one and half a dozen of the other perhaps, but int[5][5] does have > 1 dimension :-))
> > 
> > Actually, you're wrong sir.  An int[5][5] produces an array containing 5 structs, each of:
> > 
> > struct Array {
> >   size_t(or was it int?  uint?) length;
> >   void* ptr;
> > }
> > 
> > Each of those arrays then points to another array of 5 elements.
> 
> No, he was right. That struct is only used for dynamic arrays, not with
> static arrays. Static arrays are just a linear row of members stored
> in-place (but passed by reference).
> You don't need to take my word for it, look at what the compilers do:
> =====
> $ cat test.d
> import std.stdio;
> 
> int[5][5] matrix;
> 
> void main() {
>      // Initialize
>      foreach (r, inout row; matrix) {
>          foreach (c, inout elt; row) {
>              elt = r * 5 + c;
>          }
>      }
> 
>      // Access as a linear array:
>      uint* p = cast(uint*) matrix.ptr;
>          // (Note: ".ptr" is not actually stored but inserted as
>          // a constant)
>      for (size_t i = 0; i < 25; i++) {
>          writef(p[i],' ');
>      }
>      writefln();
> }
> $ dmd -run test.d
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> $ gdc -o test test.d && ./test
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> =====

Fascinating.  I thought static arrays were implemented in the same way (in terms of structure) as dynamic arrays.  I'm curious then if I declare a:

struct z { int[] y; }

static z[] x = [ { y:[1,2,3] } ];

Does this implement a static array, or a dynamic array?  I *hope* a dynamic one, as otherwise I will have to rewrite my entire Global_init routine for Walnut 2.x.  : o

> > What I believe you want is a rectangular array:
> > 
> > int[5,5], which will produce a single Array struct that you can access via taking a multiple of the first index added to the second index (or is it vice versa?)
> 
> Sorry, "int[5,5]" only compiles because of the obscure comma operator.

According to the D spec, that should compile as a rectangular array.  I haven't tried it.  I have read the spec.

See:
http://digitalmars.com/d/arrays.html
and visit "Rectangular Arrays".

PS: Walter, any chance we can get <a name=""> so we can link directly to specific parts of the spec?

April 10, 2007
"Dan" <murpsoft@hotmail.com> wrote in message news:evem9b$a9p$1@digitalmars.com...
>
> Fascinating.  I thought static arrays were implemented in the same way (in terms of structure) as dynamic arrays.  I'm curious then if I declare a:
>
> struct z { int[] y; }
>
> static z[] x = [ { y:[1,2,3] } ];
>
> Does this implement a static array, or a dynamic array?  I *hope* a dynamic one, as otherwise I will have to rewrite my entire Global_init routine for Walnut 2.x.  : o

Dynamic arrays.  Nothing in the brackets -- always dynamic.

> According to the D spec, that should compile as a rectangular array.  I haven't tried it.  I have read the spec.
>
> See:
> http://digitalmars.com/d/arrays.html
> and visit "Rectangular Arrays".

quote:
================
Fortunately, D static arrays, while using the same syntax, are implemented
as a fixed rectangular layout:
double[3][3] matrix;

declares a rectangular matrix with 3 rows and 3 columns, all contiguously in
memory. ***In other languages***, this would be called a multidimensional
array and be declared as:
double matrix[3,3];
================

Notice the "in other languages" bit ;)  (though I have no idea _what_ other languages Walter's referring to in this sentence..)

> PS: Walter, any chance we can get <a name=""> so we can link directly to specific parts of the spec?
>

I think there are some anchors, they're just not visible outside the page source.  That would be very convenient, of course.


April 10, 2007
On Mon, 09 Apr 2007 20:38:03 -0400, Dan wrote:

>> 
>> Sorry, "int[5,5]" only compiles because of the obscure comma operator.
> 
> According to the D spec, that should compile as a rectangular array.  I haven't tried it.  I have read the spec.
> 
> See:
> http://digitalmars.com/d/arrays.html
> and visit "Rectangular Arrays".

I don't read the spec that way at all.

"Fortunately, D static arrays, while using the same syntax, are implemented
 as a fixed rectangular layout:

   double[3][3] matrix;

 declares a rectangular matrix with 3 rows and 3 columns, all contiguously
 in memory.

 In other languages, this would be called a multidimensional array and
 be declared as:

   double matrix[3,3];
"

To me, this is say that D uses the [3][3] syntax but *other* languages use the [3,3] syntax.


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
10/04/2007 11:03:42 AM
April 10, 2007
Derek Parnell wrote:
> On Mon, 09 Apr 2007 20:38:03 -0400, Dan wrote:
> 
>>> Sorry, "int[5,5]" only compiles because of the obscure comma operator. 
>> According to the D spec, that should compile as a rectangular array.  I haven't tried it.  I have read the spec.
>>
>> See:
>> http://digitalmars.com/d/arrays.html
>> and visit "Rectangular Arrays".
> 
> I don't read the spec that way at all.
> 
> "Fortunately, D static arrays, while using the same syntax, are implemented
>  as a fixed rectangular layout:        double[3][3] matrix;
> 
>  declares a rectangular matrix with 3 rows and 3 columns, all contiguously
>  in memory. 
> 
>  In other languages, this would be called a multidimensional array and
>  be declared as: 
> 
>    double matrix[3,3];
> "
> 
> To me, this is say that D uses the [3][3] syntax but *other* languages use
> the [3,3] syntax.
> 
> 

Dan is not the first one to misread that particular passage in that particular way.  It needs to be rewritten.   The fact that the [3,3] appears in a code box just like other D code is hugely misleading.  It should at least be followed by an ALL CAPS comment:

   double matrix[3,3]; // NOT D CODE!

But anyway, there's really no reason for that last sentence.  Just cut it, I say.  The docs are supposed to describe what you can do in *D* and how to do it.  How other (vague and unspecified) languages do it is irrelevant.

--bb
April 10, 2007
On Tue, 10 Apr 2007 12:36:53 +0900, Bill Baxter wrote:

> But anyway, there's really no reason for that last sentence.  Just cut it, I say.  The docs are supposed to describe what you can do in *D* and how to do it.  How other (vague and unspecified) languages do it is irrelevant.

Yep.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
10/04/2007 2:38:43 PM
April 11, 2007
I don't think so...  Sorry!

Craig Black wrote:
> Graham, I was wondering if you were someone that I know.  Are you the Graham I am thinking of?
> 
> -Craig
> 
> "Graham" <grahamamacdonald@gmail.com> wrote in message news:ev7kdm$1p8e$1@digitalmars.com...
>> Why will array setting work for a 1D array, but not a 2D?  If I enter:
>>   int[5] buffer;
>>   buffer[] = 5;
>>
>> It compiles, but if I enter:
>>   int[5][5] buffer;
>>   buffer[][] = 5;
>>
>> (I tried the variation buffer[] = 5, but that didn't work either.)
>>
>> It fails to compile (in gdc), with the warning:
>>
>>   MainLoop.d:56: Error: cannot implicitly convert expression (5) of type int to int[5]
>>   Error: cannot cast int to int[5]
>>   make: *** [build/MainLoop.o] Error 1
>>
>> To my untrained eye, it looks like it would like me to pass it an int[5] to initialize the array, but that's a bit odd - I'd much rather treat it similarly to a 1D array in this case!
>>
>> Thanks,
>> graham 
> 
> 
1 2 3
Next ›   Last »