Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 29, 2011 Array initialization quiz | ||||
---|---|---|---|---|
| ||||
A little D2 quiz, of course. This was discussed some time ago, but repeating now and then such things doesn't hurt in D.learn. Do you think this little program compiles? Or do you think the D2 compiler will refuse to compile it and ask you for a cast from int -> ubyte? If this compiles (maybe because you have added a cast at line 4) do you think this program is working correctly? void main() { ubyte[256] table; foreach (ubyte i, ref x; table) x = 255 - i; } Bye, bearophile |
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | I get an infinite loop. :s |
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> I get an infinite loop. :s
In your brain, really? Is that dangerous?
Bye,
bearophile
|
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Err no? Running after compilation goes into an infinite loop on 2.056. |
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> Err no? Running after compilation goes into an infinite loop on 2.056.
Silly, this is a quiz, you need to try to answer without compiling it first :-)
Do you know why the compiler doesn't ask you for a cast, and why the run does that?
Bye,
bearophile
|
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 11/29/11, bearophile <bearophileHUGS@lycos.com> wrote: > Do you know why the compiler doesn't ask you for a cast, and why the run does that? Because foreach is broken? http://d.puremagic.com/issues/show_bug.cgi?id=4510 |
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Le 29/11/2011 04:11, Andrej Mitrovic a écrit :
> On 11/29/11, bearophile<bearophileHUGS@lycos.com> wrote:
>> Do you know why the compiler doesn't ask you for a cast, and why the run
>> does that?
>
> Because foreach is broken?
> http://d.puremagic.com/issues/show_bug.cgi?id=4510
No it has nothing to do with this bug. But actually, this exemple should generate a warning at least, or being illegal eventually. I being ubyte, the max value is 255, so you always ends up with a valid value of i and the loop never stop.
|
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | deadalnix: > No it has nothing to do with this bug. I tend to agree. > But actually, this exemple should generate a warning at least, or being illegal eventually. I'd like that code to loop on all array 256 items once, and then stop :-) Bye, bearophile |
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | I meant that in a sarcastic way. :) |
November 29, 2011 Re: Array initialization quiz | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Am 29.11.2011, 14:53 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:
> deadalnix:
>
>> No it has nothing to do with this bug.
>
> I tend to agree.
>
>
>> But actually, this exemple should
>> generate a warning at least, or being illegal eventually.
>
> I'd like that code to loop on all array 256 items once, and then stop :-)
>
> Bye,
> bearophile
Is it evil if I propose that 'i' should always be size_t and nothing else?
|
Copyright © 1999-2021 by the D Language Foundation