Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 05, 2004 array operations + some strange things | ||||
---|---|---|---|---|
| ||||
I know array operations aren't implemented yet but i can do this: int[] arr = new int[10]; arr[] = 5; //set all elements of arr to 5 and i see this code as: for(int i=0;i<arr.length;i++) { arr[i]=5; } So i wanted to try this in two dimensions and wrote int [][] p4 = new int[][7]; p4[] = new int[6]; expecting the last line to mean: for(int i=0; i<p4.length; i++) { p4[i]=new int[6]; } but it DOESN'T: It turned out to be something like: int[] temp = new int[6]; for(int i=0; i<p4.length; i++) { p4[i]=temp; } So i end up with an array where all rows point to the same array instead of a new array being allocated foreach row! Is this the expected behaviour? If so, what are the reasons? |
May 05, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | It would appear that the rvalue is only evaluated once. Issues like this show the risks with with some of semantic sugar people want with arrays. "Ivan Senji" <ivan.senji@public.srce.hr> wrote in message news:c7aob7$103l$1@digitaldaemon.com... > I know array operations aren't implemented yet but i can do this: > > int[] arr = new int[10]; > > arr[] = 5; //set all elements of arr to 5 > and i see this code as: > for(int i=0;i<arr.length;i++) > { > arr[i]=5; > } > > So i wanted to try this in two dimensions and wrote > > int [][] p4 = new int[][7]; > p4[] = new int[6]; > > expecting the last line to mean: > for(int i=0; i<p4.length; i++) > { > p4[i]=new int[6]; > } > > but it DOESN'T: > It turned out to be something like: > > int[] temp = new int[6]; > for(int i=0; i<p4.length; i++) > { > p4[i]=temp; > } > > So i end up with an array where all rows point to the same array instead of a new array being allocated foreach row! > > Is this the expected behaviour? If so, what are the reasons? > > |
May 05, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Scott Egan | "Scott Egan" <scotte@tpg.com.aux> wrote in message news:c7arno$1685$1@digitaldaemon.com... > It would appear that the rvalue is only evaluated once. > > Issues like this show the risks with with some of semantic sugar people want > with arrays. This wouldn't be an issue if the semantics were consistently defined! I don't think anyone would like to define the array where every row points to the same one like in this example. int [][] p4 = new int[][7]; p4[] = new int[6]; Rrvalue should be evaluated every time, just like it is in the spec: a[] op b is for(int i=0; i<a.length; i++) { a[i] op b; } > "Ivan Senji" <ivan.senji@public.srce.hr> wrote in message news:c7aob7$103l$1@digitaldaemon.com... > > I know array operations aren't implemented yet but i can do this: > > > > int[] arr = new int[10]; > > > > arr[] = 5; //set all elements of arr to 5 > > and i see this code as: > > for(int i=0;i<arr.length;i++) > > { > > arr[i]=5; > > } > > > > So i wanted to try this in two dimensions and wrote > > > > int [][] p4 = new int[][7]; > > p4[] = new int[6]; > > > > expecting the last line to mean: > > for(int i=0; i<p4.length; i++) > > { > > p4[i]=new int[6]; > > } > > > > but it DOESN'T: > > It turned out to be something like: > > > > int[] temp = new int[6]; > > for(int i=0; i<p4.length; i++) > > { > > p4[i]=temp; > > } > > > > So i end up with an array where all rows point to the same array instead of a new array being allocated foreach row! > > > > Is this the expected behaviour? If so, what are the reasons? > > > > > > |
May 05, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote:
> "Scott Egan" <scotte@tpg.com.aux> wrote in message news:c7arno$1685$1@digitaldaemon.com...
>> It would appear that the rvalue is only evaluated once.
>>
>> Issues like this show the risks with with some of semantic sugar people
> want
>> with arrays.
>
> This wouldn't be an issue if the semantics were consistently defined!
>
> I don't think anyone would like to define the array where every row points to the same one like in this example.
>
> int [][] p4 = new int[][7];
> p4[] = new int[6];
>
> Rrvalue should be evaluated every time, just like it is in the spec:
> a[] op b
> is
> for(int i=0; i<a.length; i++)
> {
> a[i] op b;
> }
I don't know which way it should be, but in any case, the spec should clearly define this. As far as I can see it, the specs for array operations are not very exact yet. I hope, much of this will be cleared up as soon as Walter or somebody tries to actually implement the stuff and finds the delicate cases in that course.
|
May 05, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote: > "Scott Egan" <scotte@tpg.com.aux> wrote in message > news:c7arno$1685$1@digitaldaemon.com... <snip> > Rrvalue should be evaluated every time, just like it is in the spec: > a[] op b > is > for(int i=0; i<a.length; i++) > { > a[i] op b; > } <snip> Actually, I don't think that's supposed to say anything about when or how many times b is evaluated. It's using b to mean the result, rather than the expression. Evaluating it once for each element would be analogous to the old C #define CUBE(x) ((x)*(x)*(x)) qwert = CUBE(++yuiop); i.e. it would lead to both confusion and potential undefined behaviour. The alternative would be to assign to each element a copy of the expression, i.e. (again using b to mean the result) a[] op b; is for (int i = 0; i < a.length; i++) { a[i] op b.dup; } But you could go on arguing over when this is desired and when it isn't, when you also consider AAs and class objects.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
May 05, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:c7b661$1mqd$1@digitaldaemon.com... > Ivan Senji wrote: > > > "Scott Egan" <scotte@tpg.com.aux> wrote in message news:c7arno$1685$1@digitaldaemon.com... > <snip> > > Rrvalue should be evaluated every time, just like it is in the spec: > > a[] op b > > is > > for(int i=0; i<a.length; i++) > > { > > a[i] op b; > > } > <snip> > > Actually, I don't think that's supposed to say anything about when or how many times b is evaluated. It's using b to mean the result, rather than the expression. But when array operations get implemented it will mean the expression. For example with int[] a,b,c; c[] = a[] + b[]; means for(int i=0;i<c.length; i++) { c[i]=a[i]+b[i]; } And the expression is evaluated for every index i. > Evaluating it once for each element would be analogous to the old C > > #define CUBE(x) ((x)*(x)*(x)) > qwert = CUBE(++yuiop); > > i.e. it would lead to both confusion and potential undefined behaviour. > > The alternative would be to assign to each element a copy of the expression, i.e. (again using b to mean the result) > > a[] op b; > is > for (int i = 0; i < a.length; i++) { > a[i] op b.dup; > } > > But you could go on arguing over when this is desired and when it isn't, when you also consider AAs and class objects.... > > Stewart. > > -- > My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote: <snip> > But when array operations get implemented it will mean the expression. > > For example > with int[] a,b,c; > c[] = a[] + b[]; > > means > > for(int i=0;i<c.length; i++) > { > c[i]=a[i]+b[i]; > } > > And the expression is evaluated for every index i. <snip top of upside-down reply> Not _the_ expression, but _an_ expression. That expression is b[i], where b remains the _result_ of the expression in the code. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:c7d6dp$1o8d$1@digitaldaemon.com... > Ivan Senji wrote: > <snip> > > But when array operations get implemented it will mean the expression. > > > > For example > > with int[] a,b,c; > > c[] = a[] + b[]; > > > > means > > > > for(int i=0;i<c.length; i++) > > { > > c[i]=a[i]+b[i]; > > } > > > > And the expression is evaluated for every index i. > <snip top of upside-down reply> > > Not _the_ expression, but _an_ expression. That expression is b[i], where b remains the _result_ of the expression in the code. I'm sorry but my English is not that great and i didn't undesrtand what areyou trying to say. It would be nice if there was a syntax to ensure that the expression on the right is evaluated every time. For example int[] x = new int[30]; int i=0; x[!] = i++; This would fill x with numbers form 0 to 29 what isn't currently possible this easy. I was also thinking (when we get array operations one day) that it would be great to have access to the index used in for. i could then write x[] = index(0) * 3; filling x with 0, 3,6... another thing i tried to do today is: int[][][] x; x = new int[][][10]; x[] = new int[][30]; //this doesn't work the expected way // maybe x[!] = new int[][30]; but this isn't even possible: x[][] = new int[15]; // x[!][!] = new int[15]; would also be nice this would mean for(int i=0; i<x.length;i++) for(int j=0;j<x[i].length; j++) { x[i][j] = new int[15]; } But if we could do even: x[!][!][!] = index(0)+index(1)+index(2); that would be a very easy way to fill arrays with some pattern. All this i have said are only my daydreams :) about future D-arrays. But right now they are allready much better then (for example) C/C++ arrays. There are also many things that could be done to make them even better and more powerfull tool :) > Stewart. > > -- > My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Here goes another strange thing. int[][][] x = new int[][][10]; x[] = new int[][20]; //but not with the expected meaning. x[][] = new int[][21]; //possible x[][][] = new int[][22]; //also possible x[][][][] = new int[][23]; //still possible... type of x is int[][][] type of x[]is int[][] type of x[][] is int[][] type of x[][][] is int[][] |
May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | In article <c7dp1g$2pv9$1@digitaldaemon.com>, ivan.senji@public.srce.hr says... > >Here goes another strange thing. >int[][][] x = new int[][][10]; >x[] = new int[][20]; //but not with the expected meaning. >x[][] = new int[][21]; //possible >x[][][] = new int[][22]; //also possible >x[][][][] = new int[][23]; //still possible... > >type of x is int[][][] >type of x[]is int[][] >type of x[][] is int[][] >type of x[][][] is int[][] > > x[] is shorthand for x[0 .. x.length]. So there's nothing wrong with x[0 .. x.length][0 .. x.length][0 .. x.length] nor with x[][][]. It's just re-slicing the same memory. -- Christopher E. Miller |
Copyright © 1999-2021 by the D Language Foundation