Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 19, 2004 [BUG] compiler segfault | ||||
---|---|---|---|---|
| ||||
void main() { int i = 10; while ( false, (i-- > 0) ) { printf("i = %.*s\n", i); } } $ dmd T -I~/dmd/src/phobos Segmentation fault should it compile at all? Ant |
March 19, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | On Fri, 19 Mar 2004 17:38:36 -0500, Ant wrote: grrr :( I meant: void main() { int i = 10; while ( false, (i-- > 0) ) { printf("i = %d\n", i); } } $ dmd T -I~/dmd/src/phobos $ T i = 9 i = 8 i = 7 i = 6 i = 5 i = 4 i = 3 i = 2 i = 1 i = 0 is this valid? "while ( false, (i-- > 0) )" Ant |
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | In article <pan.2004.03.19.22.44.02.7718@yahoo.ca>, Ant says... > >On Fri, 19 Mar 2004 17:38:36 -0500, Ant wrote: > >grrr :( > >I meant: >void main() >{ > int i = 10; > while ( false, (i-- > 0) ) > { > printf("i = %d\n", i); > } >} > >$ dmd T -I~/dmd/src/phobos >$ T > >i = 9 >i = 8 >i = 7 >i = 6 >i = 5 >i = 4 >i = 3 >i = 2 >i = 1 >i = 0 > >is this valid? "while ( false, (i-- > 0) )" > >Ant > |
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | "Ant" <duitoolkit@yahoo.ca> wrote in message news:pan.2004.03.19.22.44.02.7718@yahoo.ca... > On Fri, 19 Mar 2004 17:38:36 -0500, Ant wrote: > > grrr :( > > I meant: > void main() > { > int i = 10; > while ( false, (i-- > 0) ) > { > printf("i = %d\n", i); > } > } > > $ dmd T -I~/dmd/src/phobos > $ T > > i = 9 > i = 8 > i = 7 > i = 6 > i = 5 > i = 4 > i = 3 > i = 2 > i = 1 > i = 0 > > is this valid? "while ( false, (i-- > 0) )" Yes. |
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sat, 20 Mar 2004 12:16:07 -0800, Walter wrote:
>
> "Ant" <duitoolkit@yahoo.ca> wrote in message
>>
>> is this valid? "while ( false, (i-- > 0) )"
>
> Yes.
thank you.
interesting:
(from the manual)
Expression:
AssignExpression
AssignExpression , Expression
Anyone cares to explain why is it valid
and what the use of
"while ( false, (i-- > 0) )"
?
thanks.
what I was trying to do was something like
"while ( !found && (i<lines.length) )"
I don't know why I typed "," instead of "&&"
but it cost me quite a bit of time to find the problem...
Ant
|
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | In C, (expr1, expr2, expr3) is a valid expression, and evaluates to the value of the rightmost subexpression. This is probably what is being carried forward. So (!found,i-- > 0) would evaluate !found, then replace its evaluation with that of i-- > 0, and use that - so it is NOT the same as (!found && i--> 0) There are possible uses of side-effects, so something like (i--,i++ > 0) would check for i-1 > 0 without changing it. Evaluation of a list of expressions like this should always be left-to-right, with only the side-effects being carried forward, the valuation is replaced with the next one until the rightmost is reached. In article <pan.2004.03.20.20.33.33.170646@yahoo.ca>, Ant says... > >On Sat, 20 Mar 2004 12:16:07 -0800, Walter wrote: > >> >> "Ant" <duitoolkit@yahoo.ca> wrote in message >>> >>> is this valid? "while ( false, (i-- > 0) )" >> >> Yes. > >thank you. > >interesting: > >(from the manual) >Expression: > AssignExpression > AssignExpression , Expression > > >Anyone cares to explain why is it valid >and what the use of >"while ( false, (i-- > 0) )" >? > >thanks. > >what I was trying to do was something like > >"while ( !found && (i<lines.length) )" > >I don't know why I typed "," instead of "&&" >but it cost me quite a bit of time to find the problem... > >Ant > |
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | I tried a post a minute ago which didn't seem to take, so if this is a duplicate, at least I hope it's a better thought out one... In C, (expr1,expr2,expr3) would be evaluated left to right, with expr1 evaluated, then expr2 replacing the value, then expr3 which would be the final value of the expression. The value of (!found, i-- > 0) would be that of i-- > 0 with the !found value effectively ignored. Thus it's not the same as (!found && i-- > 0). There are uses where the side-effects of evaluating the left-side expressions are meaningful. This is not a really useful example, but (i--,i++ > 0) would test for i-1 > 0 without modifying the value of i. D seems to allow if (a=1,b=2,c=3,d=a+b+c,d == 6) and evaluates it correctly here where it is not really appropriate, but does not allow the same content in an assert statement where something like it might be useful. In article <pan.2004.03.20.20.33.33.170646@yahoo.ca>, Ant says... > >On Sat, 20 Mar 2004 12:16:07 -0800, Walter wrote: > >> >> "Ant" <duitoolkit@yahoo.ca> wrote in message >>> >>> is this valid? "while ( false, (i-- > 0) )" >> >> Yes. > >thank you. > >interesting: > >(from the manual) >Expression: > AssignExpression > AssignExpression , Expression > > >Anyone cares to explain why is it valid >and what the use of >"while ( false, (i-- > 0) )" >? > >thanks. > >what I was trying to do was something like > >"while ( !found && (i<lines.length) )" > >I don't know why I typed "," instead of "&&" >but it cost me quite a bit of time to find the problem... > >Ant > |
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to larry cowan | On Sat, 20 Mar 2004 21:29:46 +0000, larry cowan wrote: > In C, > (expr1, expr2, expr3) > is a valid expression, and evaluates to the value of the rightmost > subexpression. [...] > side-effects [...] > side-effects Thank you. I didn't know. I believe I got it. should we vote to drop it!?... Ant |
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to larry cowan | On Sat, 20 Mar 2004 21:56:23 +0000, larry cowan wrote:
> I tried a post a minute ago which didn't seem to take, so if this is a duplicate, at least I hope it's a better thought out one...
>
> In C,
> (expr1,expr2,expr3)
> would be evaluated left to right, with expr1 evaluated, then expr2 replacing the
> value, then expr3 which would be the final value of the expression.
>
> The value of (!found, i-- > 0) would be that of i-- > 0 with the !found value
> effectively ignored. Thus it's not the same as (!found && i-- > 0).
>
> There are uses where the side-effects of evaluating the left-side expressions are meaningful. This is not a really useful example, but (i--,i++ > 0) would test for i-1 > 0 without modifying the value of i.
>
> D seems to allow
>
> if (a=1,b=2,c=3,d=a+b+c,d == 6)
>
> and evaluates it correctly here where it is not really appropriate, but does not allow the same content in an assert statement where something like it might be useful.
>
thanks, maybe I can see the benefit of that some day...
void main()
{
assert(false, 0);
}
$ dmd T -I~/dmd/src/phobos
T.d(3): found ',' when expecting ')'
T.d(3): found '0' when expecting ';' following 'statement'
T.d(3): found ')' instead of statement
Ant
|
March 20, 2004 Re: [BUG] compiler segfault - NOT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | Ant wrote: > On Sat, 20 Mar 2004 21:29:46 +0000, larry cowan wrote: > > >>In C, (expr1, expr2, expr3) >>is a valid expression, and evaluates to the value of the rightmost >>subexpression. > > [...] > >>side-effects > > [...] > >>side-effects > > > Thank you. > I didn't know. > I believe I got it. > > should we vote to drop it!?... > > Ant > I would, except it's common practice to use in a for loop: for(i = 0; i < 5; i++, s++) { ... } The D way could be different, but what? for(i = 0; i < 5; {i++; s++;}) { ... } That's not too bad, especially since the comma operator can be a bit tricky to use. -- Christopher E. Miller |
Copyright © 1999-2021 by the D Language Foundation