www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array-wise assignment on unallocated array

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
There's an example in TDPL showing array-wise expressions. I've modified the
code and removed the allocation. This will compile and run without exceptions:

void main() {
    auto a = [0.5, -0.5, 1.5, 2];
    auto b = [3.5, 5.5, 4.5, -1];
    
    double[] c;
    c[] = (a[] + b[]) / 2;
}

I think assignments to unallocated arrays should throw a runtime exception, or
at least give out a compile warning. Compare this to this code which throws a
RangeError:

import std.stdio;

void main() {
    double[] c;
    c[0] = 4;
}

And the equivalent array-wise assignment which doesn't throw exceptions:

import std.stdio;

void main() {
    double[] c;
    c[] = 4;
}

Bugzilla-worthy?
Jul 27 2010
next sibling parent Kagamin <spam here.lot> writes:
Andrej Mitrovic Wrote:

 I think assignments to unallocated arrays should throw a runtime exception, or
at least give out a compile warning.
array operation is not an array assignment, it's an operation on existing array. null array is an array of length 0. A warning may help here.
Jul 27 2010
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
Andrej Mitrovic wrote:
 There's an example in TDPL showing array-wise expressions. I've modified the
code and removed the allocation. This will compile and run without exceptions:
 
 void main() {
     auto a = [0.5, -0.5, 1.5, 2];
     auto b = [3.5, 5.5, 4.5, -1];
     
     double[] c;
     c[] = (a[] + b[]) / 2;
 }
 
 I think assignments to unallocated arrays should throw a runtime exception, or
at least give out a compile warning. Compare this to this code which throws a
RangeError:
 
 import std.stdio;
 
 void main() {
     double[] c;
     c[0] = 4;
 }
 
 And the equivalent array-wise assignment which doesn't throw exceptions:
 
 import std.stdio;
 
 void main() {
     double[] c;
     c[] = 4;
 }
 
 Bugzilla-worthy?
Yes, it should throw an out of bounds error.
Jul 27 2010
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Thanks. Filed in bugzilla http://d.puremagic.com/issues/show_bug.cgi?id=4521

Walter Bright Wrote:

 Andrej Mitrovic wrote:
 There's an example in TDPL showing array-wise expressions. I've modified the
code and removed the allocation. This will compile and run without exceptions:
 
 void main() {
     auto a = [0.5, -0.5, 1.5, 2];
     auto b = [3.5, 5.5, 4.5, -1];
     
     double[] c;
     c[] = (a[] + b[]) / 2;
 }
 
 I think assignments to unallocated arrays should throw a runtime exception, or
at least give out a compile warning. Compare this to this code which throws a
RangeError:
 
 import std.stdio;
 
 void main() {
     double[] c;
     c[0] = 4;
 }
 
 And the equivalent array-wise assignment which doesn't throw exceptions:
 
 import std.stdio;
 
 void main() {
     double[] c;
     c[] = 4;
 }
 
 Bugzilla-worthy?
Yes, it should throw an out of bounds error.
Jul 27 2010