digitalmars.D.bugs - AA of AAs -- ArrayBoundsError
- nobody (12/12) Oct 29 2007 The following code results in an ArrayBoundsError with my v1.015
- Matti Niemenmaa (8/21) Oct 29 2007 The problem is on the right hand side of the assignment, namely on the a...
- nobody (49/71) Oct 29 2007 I really messed up the example. You are correct that my example
- Matti Niemenmaa (11/70) Oct 29 2007 This is essentially the same case as your original example. table[i].len...
The following code results in an ArrayBoundsError with my v1.015 compiler: typedef size_t IA; typedef size_t IB; typedef uint V; void main(char[][] args) { V[IB] [IA] test; test[cast(IA) test.length] [cast(IB) test[cast(IA) test.length].length] = cast(V) (test.length * test[cast(IA) test.length].length); }
Oct 29 2007
nobody wrote:The following code results in an ArrayBoundsError with my v1.015 compiler: typedef size_t IA; typedef size_t IB; typedef uint V; void main(char[][] args) { V[IB] [IA] test; test[cast(IA) test.length] [cast(IB) test[cast(IA) test.length].length] = cast(V) (test.length * test[cast(IA) test.length].length); }The problem is on the right hand side of the assignment, namely on the access test[cast(IA) test.length]. The AA test is empty, so retrieving any element will fail with an ArrayBoundsError. Assign test[1][1] = something; first, and then it will work. (1, because test.length will be 1 after the assignment.) -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Oct 29 2007
Matti Niemenmaa wrote:nobody wrote:I really messed up the example. You are correct that my example should not work. However, both the following add functions fail when I think they shouldn't: import std.stdio; typedef size_t I; struct Si { uint val; } struct Sa { Si[I][I] table; void add(I i, Si si) { table[i][cast(I) table[i].length] = si; } } struct Sb { Si[][I] table; void add(I i, Si si) { if(table[i] is null) table[i] = new Si[0]; table[i].length = table[i].length + 1; table[i][table[i].length - 1] = si; } } void main(char[][] args) { Si si; Sa sa; try { sa.add(cast(I) 17, si); } catch(Error e) { writef("Sa failed\n"); } Sb sb; try { sb.add(cast(I) 17, si); } catch(Error e) { writef("Sb failed\n"); } } ---------------------------------------------------------------- Sa failed SB failedThe following code results in an ArrayBoundsError with my v1.015 compiler: typedef size_t IA; typedef size_t IB; typedef uint V; void main(char[][] args) { V[IB] [IA] test; test[cast(IA) test.length] [cast(IB) test[cast(IA) test.length].length] = cast(V) (test.length * test[cast(IA) test.length].length); }The problem is on the right hand side of the assignment, namely on the access test[cast(IA) test.length]. The AA test is empty, so retrieving any element will fail with an ArrayBoundsError. Assign test[1][1] = something; first, and then it will work. (1, because test.length will be 1 after the assignment.)
Oct 29 2007
nobody wrote:Matti Niemenmaa wrote:This is essentially the same case as your original example. table[i].length results in an error because table[i] does not exist yet.nobody wrote:I really messed up the example. You are correct that my example should not work. However, both the following add functions fail when I think they shouldn't: import std.stdio; typedef size_t I; struct Si { uint val; } struct Sa { Si[I][I] table; void add(I i, Si si) { table[i][cast(I) table[i].length] = si; } }The following code results in an ArrayBoundsError with my v1.015 compiler: typedef size_t IA; typedef size_t IB; typedef uint V; void main(char[][] args) { V[IB] [IA] test; test[cast(IA) test.length] [cast(IB) test[cast(IA) test.length].length] = cast(V) (test.length * test[cast(IA) test.length].length); }The problem is on the right hand side of the assignment, namely on the access test[cast(IA) test.length]. The AA test is empty, so retrieving any element will fail with an ArrayBoundsError. Assign test[1][1] = something; first, and then it will work. (1, because test.length will be 1 after the assignment.)struct Sb { Si[][I] table; void add(I i, Si si) { if(table[i] is null) table[i] = new Si[0]; table[i].length = table[i].length + 1; table[i][table[i].length - 1] = si; } }As above, "if (table[i] is null)" results in an error because table[i] does not exist yet. What would, however, work is "if (!(i in table))". (You might have noticed this if you would have output the error instead of just "Sb failed": the ArrayBoundsError is on the line where the if statement is.) BTW, you should post these on the digitalmars.D.learn newsgroup unless you're 100% sure they're bugs. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Oct 29 2007