[Issue 3847] To avoid a C code bug

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 23 18:39:13 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3847


Ellery Newcomer <ellery-newcomer at utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ellery-newcomer at utulsa.edu


--- Comment #1 from Ellery Newcomer <ellery-newcomer at utulsa.edu> 2010-02-23 18:39:12 PST ---
(In reply to comment #0)
> What does this D2 program print, and why?
> 
> 
> import std.stdio;
> 
> bool thirdElementIsThree(int[] a) {
>     return a.length >= 3 & a[2] == 3;
> }
> 
> void main() {
>     int[][] tests = [[6, 5, 4, 3, 2, 1],
>                      [1, 2],
>                      [1, 2, 3],
>                      [1, 2, 3, 4 ],
>                      [1]];
>     int n = 0;
> 
>     try {
>         int i = 0;
>         while (true) {
>             if (thirdElementIsThree(tests[i++]))
>                 n++;
>         }
>     } catch(Error e) {
>         // No more tests to process
>     }
> 
>     writeln(n); // prints?
> }


Ooh! Ooh! I know! it segfaults because the loop doesn't terminate and
eventually test[i++] attempts to access memory not allocated to that process!

We're talking about things that would happen in C, right?


Personally, I prefer and,or, etc over &&,||, etc because they take less finger
gymnastics to type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list