digitalmars.D - arr.length as lvalue
- Lars Ivar Igesund (9/9) Sep 20 2004 Why do
- Burton Radons (4/18) Sep 20 2004 It was introduced to prevent:
- Regan Heath (19/37) Sep 20 2004 I don't think that has an undefined result, AFAIKS:
- Russ Lewis (12/29) Sep 20 2004 Since it's post increment, isn't the sequence actually:
- Regan Heath (16/46) Sep 20 2004 Oops, yes, you're right.
- Sha Chancellor (8/65) Sep 20 2004 I think it's rather silly that a so called attribute has a special
- Russ Lewis (8/24) Sep 21 2004 Well, you're not the first to state this viewpoint. I, for one, agree
- Id (21/27) Sep 21 2004 Currently, dynamic arrays are composed of a:
- Arcane Jill (12/33) Sep 22 2004 Which two, exactly?
- Sean Kelly (5/27) Sep 20 2004 I think the correct expression would be this:
- Burton Radons (10/21) Sep 20 2004 Nope; the comp.lang.c FAQ has a whole section essentially devoted to the...
- Sjoerd van Leent (20/43) Sep 20 2004 It seems also that arr.length++ is rather pointless, because when adding...
- Lars Ivar Igesund (4/55) Sep 21 2004 Read "Setting Dynamic Array Length" here:
- Russ Lewis (3/19) Sep 20 2004 This is working as designed (see other posts), but the error message is
Why do arr.length = arr.length + 1; work when arr.length += 1; and arr.length++; don't ("arr.length is not an lvalue")? AFAIK, arr.length is an lvalue in the first statement, too. Lars Ivar Igesund
Sep 20 2004
Lars Ivar Igesund wrote:Why do arr.length = arr.length + 1; work when arr.length += 1; and arr.length++; don't ("arr.length is not an lvalue")? AFAIK, arr.length is an lvalue in the first statement, too.It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.
Sep 20 2004
On Mon, 20 Sep 2004 13:58:40 -0700, Burton Radons <burton-radons shaw.ca> wrote:Lars Ivar Igesund wrote:I don't think that has an undefined result, AFAIKS: - the value of arr.length is taken (L) - arr.length is incremented. - foo is assigned to arr[L]. eg. L = arr.length; arr.length++; arr[L] = foo; If however the expression 'foo' modified arr.length you'd have a different story, so I assume that is what you meant? In which case can't we rely on precedence rules applying i.e. the RHS is evaluated then the LHS or something. I say this because I think arr.length++ is intuitive and therefore desirable. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/Why do arr.length = arr.length + 1; work when arr.length += 1; and arr.length++; don't ("arr.length is not an lvalue")? AFAIK, arr.length is an lvalue in the first statement, too.It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.
Sep 20 2004
Regan Heath wrote:Since it's post increment, isn't the sequence actually: arr[arr.length] = foo; arr.length = arr.length + 1; which is obviously broken because you're exceeding the bounds of the array? Anyhow, what Walter has said is that he doesn't want to have arr.length++; because it makes it look like setting .length is as simple as addition, when actually it's a call to a fairly complex function. Please, let's not argue this (again). It's been argued at length - several times. Check the old posts, in both this newsgroup and the older one.It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.I don't think that has an undefined result, AFAIKS: - the value of arr.length is taken (L) - arr.length is incremented. - foo is assigned to arr[L]. eg. L = arr.length; arr.length++; arr[L] = foo;
Sep 20 2004
On Mon, 20 Sep 2004 14:24:10 -0700, Russ Lewis <spamhole-2001-07-16 deming-os.org> wrote:Regan Heath wrote:Oops, yes, you're right. In that case this restriction does stop this particular bug. Why is: arr.length += 100; restricted? it's probably the more useful of the two.Since it's post increment, isn't the sequence actually: arr[arr.length] = foo; arr.length = arr.length + 1; which is obviously broken because you're exceeding the bounds of the array?It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.I don't think that has an undefined result, AFAIKS: - the value of arr.length is taken (L) - arr.length is incremented. - foo is assigned to arr[L]. eg. L = arr.length; arr.length++; arr[L] = foo;Anyhow, what Walter has said is that he doesn't want to have arr.length++; because it makes it look like setting .length is as simple as addition, when actually it's a call to a fairly complex function.Sure, but I don't think forcing people to use arr.length = arr.length + 1 causes anyone to realise that. More likely they just think WTF?! why can't I use arr.length++; I know I did.Please, let's not argue this (again). It's been argued at length - several times.What that pun/joke intentional?Check the old posts, in both this newsgroup and the older one.Ok, but can you answer my little question above and same me/everyone who does not know already the trouble of searching. Thanks in advance. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Sep 20 2004
In article <opsenbajbk5a2sq9 digitalmars.com>, Regan Heath <regan netwin.co.nz> wrote:On Mon, 20 Sep 2004 14:24:10 -0700, Russ Lewis <spamhole-2001-07-16 deming-os.org> wrote:I think it's rather silly that a so called attribute has a special behavior here. It should be obvious that it's a function and not an lvalue if it is indeed a function. arr.setLength() or something would be much clearer. Because arr.length = arr.length + 1 makes it look like it's simple addition.....Regan Heath wrote:Oops, yes, you're right. In that case this restriction does stop this particular bug. Why is: arr.length += 100; restricted? it's probably the more useful of the two.Since it's post increment, isn't the sequence actually: arr[arr.length] = foo; arr.length = arr.length + 1; which is obviously broken because you're exceeding the bounds of the array?It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.I don't think that has an undefined result, AFAIKS: - the value of arr.length is taken (L) - arr.length is incremented. - foo is assigned to arr[L]. eg. L = arr.length; arr.length++; arr[L] = foo;Anyhow, what Walter has said is that he doesn't want to have arr.length++; because it makes it look like setting .length is as simple as addition, when actually it's a call to a fairly complex function.Sure, but I don't think forcing people to use arr.length = arr.length + 1 causes anyone to realise that. More likely they just think WTF?! why can't I use arr.length++; I know I did.Please, let's not argue this (again). It's been argued at length - several times.What that pun/joke intentional?Check the old posts, in both this newsgroup and the older one.Ok, but can you answer my little question above and same me/everyone who does not know already the trouble of searching. Thanks in advance. Regan
Sep 20 2004
Regan Heath wrote:Anyhow, what Walter has said is that he doesn't want to have arr.length++; because it makes it look like setting .length is as simple as addition, when actually it's a call to a fairly complex function.Sure, but I don't think forcing people to use arr.length = arr.length + 1 causes anyone to realise that. More likely they just think WTF?! why can't I use arr.length++; I know I did.No, but I wish it had been! :)Please, let's not argue this (again). It's been argued at length - several times.What that pun/joke intentional?Well, you're not the first to state this viewpoint. I, for one, agree with you. But only Big W can make the final language decisions. One thing in favor of his position, though, is that we could add support for the syntax later. If Walter allowed it now and we decided later that it was a bad idea, it would be almost impossible to remove from the language.Check the old posts, in both this newsgroup and the older one.Ok, but can you answer my little question above and same me/everyone who does not know already the trouble of searching.
Sep 21 2004
In article <cipnh7$1g5n$1 digitaldaemon.com>, Russ Lewis says...Regan Heath wrote:Currently, dynamic arrays are composed of a: "length" and a "pointer to the array data". I've thought, what if we used this implementation?: "reserved length" -> "array length" -> "pointer to the array data" NOTE: ("reserved" is recommended to be a power of 2, and always >= array length) This implementation would increase the overhead of the array definition and probably break backwards compatibility, yet in most cases this would make concatenation a child's play in most cases, as you could do like this example: int[] a=new int[14]; //reserved=32(amount decided by the compiler), length=14, pointer=(somewhere) int b=a[15]; //Error, Out Of Bounds array.length=19; //Allocate memory? naaah! two CPU instructions and done! :) //reserved=32, length 19, pointer=(somewhere) array.length++; //(length+1)< reserved, so we got another cheap resize ;) //reserved=32, length 20, pointer=(somewhere) array.length=80; // (80 > reserved) Memory allocation is needed...the typical slow resize :( . // reserved=128, length 80, pointer=(whatever) array.reserved=5; //lol, nice hack attempt, but no. You can't access this. What do you say?Anyhow, what Walter has said is that he doesn't want to have arr.length++; because it makes it look like setting .length is as simple as addition, when actually it's a call to a fairly complex function.
Sep 21 2004
In article <ciq8jb$2nd8$1 digitaldaemon.com>, Id says...Currently, dynamic arrays are composed of a: "length" and a "pointer to the array data". I've thought, what if we used this implementation?: "reserved length" -> "array length" -> "pointer to the array data" NOTE: ("reserved" is recommended to be a power of 2, and always >= array length)So what structure would a[1..4] return?This implementation would increase the overhead of the array definition and probably break backwards compatibility, yet in most cases this would make concatenation a child's play in most cases, as you could do like this example: int[] a=new int[14]; //reserved=32(amount decided by the compiler), length=14, pointer=(somewhere) int b=a[15]; //Error, Out Of Bounds array.length=19; //Allocate memory? naaah! two CPU instructions and done! :) //reserved=32, length 19, pointer=(somewhere)Which two, exactly?array.length++; //(length+1)< reserved, so we got another cheap resize ;) //reserved=32, length 20, pointer=(somewhere) array.length=80; // (80 > reserved) Memory allocation is needed...the typical slow resize :( . // reserved=128, length 80, pointer=(whatever) array.reserved=5; //lol, nice hack attempt, but no. You can't access this.Yes you can. A union does the trick nicely.What do you say?Did you forget about: I think it would break way too much. Jill
Sep 22 2004
In article <cinhpr$574$1 digitaldaemon.com>, Russ Lewis says...Regan Heath wrote:I think the correct expression would be this: arr[++arr.length - 1] = foo; Which just reeks of undefined behavior. SeanSince it's post increment, isn't the sequence actually: arr[arr.length] = foo; arr.length = arr.length + 1; which is obviously broken because you're exceeding the bounds of the array?It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.I don't think that has an undefined result, AFAIKS: - the value of arr.length is taken (L) - arr.length is incremented. - foo is assigned to arr[L]. eg. L = arr.length; arr.length++; arr[L] = foo;
Sep 20 2004
Regan Heath wrote:I don't think that has an undefined result, AFAIKS: - the value of arr.length is taken (L) - arr.length is incremented. - foo is assigned to arr[L].Nope; the comp.lang.c FAQ has a whole section essentially devoted to the subject (http://www.eskimo.com/~scs/C-faq/s3.html).If however the expression 'foo' modified arr.length you'd have a different story, so I assume that is what you meant?In which case can't we rely on precedence rules applying i.e. the RHS is evaluated then the LHS or something.Neither lvalue/rvalue nor precedence have any effect on order of evaluation; only the sequence point.I say this because I think arr.length++ is intuitive and therefore desirable.I agree that it's a silly error, since it's not erroneous in most cases. Making the compiler identify the special case the error was built for or introducing logic to discover undefined expressions (like GCC has) would be better. That'll have to wait until after the bug fixes and the standard, though.
Sep 20 2004
Burton Radons wrote:Lars Ivar Igesund wrote:It seems also that arr.length++ is rather pointless, because when adding a new item to an array, it can be done with the append operator: int main (char[][] args) { byte[] ba; for(byte b = 0; b < 10; b++) { ba ~= b; } // prints bytes: 0 1 2 3 4 5 6 7 8 9 printf("bytes: "); foreach(byte b; ba) { printf("%d ", b); } printf(\n); return 1; } Regards, SjoerdWhy do arr.length = arr.length + 1; work when arr.length += 1; and arr.length++; don't ("arr.length is not an lvalue")? AFAIK, arr.length is an lvalue in the first statement, too.It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.
Sep 20 2004
Sjoerd van Leent wrote:Burton Radons wrote:Read "Setting Dynamic Array Length" here: http://www.digitalmars.com/d/arrays.html Lars Ivar IgesundLars Ivar Igesund wrote:It seems also that arr.length++ is rather pointless, because when adding a new item to an array, it can be done with the append operator: int main (char[][] args) { byte[] ba; for(byte b = 0; b < 10; b++) { ba ~= b; } // prints bytes: 0 1 2 3 4 5 6 7 8 9 printf("bytes: "); foreach(byte b; ba) { printf("%d ", b); } printf(\n); return 1; } Regards, SjoerdWhy do arr.length = arr.length + 1; work when arr.length += 1; and arr.length++; don't ("arr.length is not an lvalue")? AFAIK, arr.length is an lvalue in the first statement, too.It was introduced to prevent: arr [arr.length ++] = foo; Which has an undefined result.
Sep 21 2004
Lars Ivar Igesund wrote:Why do arr.length = arr.length + 1; work when arr.length += 1; and arr.length++; don't ("arr.length is not an lvalue")? AFAIK, arr.length is an lvalue in the first statement, too. Lars Ivar IgesundThis is working as designed (see other posts), but the error message is misleading. I posted a message about it in the bugs newsgroup.
Sep 20 2004