www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - range violation

reply Dr.Smith <none 4now.com> writes:
With multidimensional arrays greater than 150x150, I get a range violation at
run time: "core.exception.RangeError pweight(54): Range violation"
Is this a bug? Is there a work-around?
Feb 28 2011
parent reply Nick Treleaven <nospam example.net> writes:
On Mon, 28 Feb 2011 16:07:41 +0000, Dr.Smith wrote:

 With multidimensional arrays greater than 150x150, I get a range
 violation at run time: "core.exception.RangeError pweight(54): Range
 violation" Is this a bug? Is there a work-around?

Can you show some code or a test case? My sample below seems to work fine (but I'm still using an older dmd - v2.049): int[151][151] x; x[150][150] = 7; writeln(x[150][150]);
Feb 28 2011
next sibling parent reply Dr.Smith <not 4now.com> writes:
For example:

double [string] data;
double [200][1000] data2;

for(int i = 0; i < 200; i++) {
    for(int j = 0; j < 1000; j++) {

      // fake multi-dim works
      string str = to!string(i) ~ "," ~ to!string(j);
      data[str] = someNumber;

      // real multi-dim does not work
      data2[i][j] = someNumber;
    }
}
Feb 28 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Dr.Smith:

 For example:
 
 double [string] data;
 double [200][1000] data2;
 
 for(int i = 0; i < 200; i++) {
     for(int j = 0; j < 1000; j++) {
 
       // fake multi-dim works
       string str = to!string(i) ~ "," ~ to!string(j);
       data[str] = someNumber;
 
       // real multi-dim does not work
       data2[i][j] = someNumber;
     }
 }

You receive the same stack overflow error with this simpler code: void main() { double[200][1000] a; } Keep in mind this is a fixed-sized array, so it's allocated on the stack. On Windows with DMD if you add a switch like this, to increase max stack size, that code works: -L/STACK:10000000 Bye, bearophile
Feb 28 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
That's because data2 has length 1000, each of which has an array of
length 200. Not the other way around. This works:

double[200][1000] data2;

void main()
{
    for(int i = 0; i < 1000; i++) {
        for(int j = 0; j < 200; j++) {
         data2[i][j] = 4.0;
       }
    }
}
Feb 28 2011