开发者

Running out of memory for 2D arrays in C++/CLI?

开发者 https://www.devze.com 2023-03-14 07:49 出处:网络
I\'m dealing with 10 of arrays, some of which are doubles 1024x1392. I\'ve tried to dynamically allocate them on the heap with:

I'm dealing with 10 of arrays, some of which are doubles 1024x1392.

I've tried to dynamically allocate them on the heap with:

double **x_array;

    x_array = new double*[NUM_ROWS];
for(int i=0; i < NUM_ROWS; i++) {
    x_array[i] = new double[NUM_COLS];
}

        for(int ix=0; ix < NUM_COLS; ix++) {
            for(int iy=0; iy < NUM_ROWS; iy++) {
                x_array[ix][iy]=(x1y1*(ix+1) + x2y1*(iy+1) + x3y1)开发者_如何学JAVA;
                //y_array[ix][iy]=(x1y2*(ix+1) + x2y2*(iy+1) + x3y2);
            }
        }
    }

but I still get errors saying

unhandled exception: System.Runtime.InteropServices.SEGException: External Component has thrown an exception. at line 106

and 106 is where I begin initializing the array in the code above:

    x_array = new double*[NUM_ROWS];

Am I really running out of space, or am I doing something wrong?


You have your array indices transposed:

    for(int ix=0; ix < NUM_COLS; ix++) {
        for(int iy=0; iy < NUM_ROWS; iy++) {
            x_array[ix][iy]=(x1y1*(ix+1) + x2y1*(iy+1) + x3y1);

should be:

    for(int iy=0; iy < NUM_ROWS; iy++) {
        for(int ix=0; ix < NUM_COLS; ix++) {
            x_array[iy][ix]=(x1y1*(ix+1) + x2y1*(iy+1) + x3y1);

or if you really have to keep the cache-hostile loop ordering for some reason:

    for(int ix=0; ix < NUM_COLS; ix++) {
        for(int iy=0; iy < NUM_ROWS; iy++) {
            x_array[iy][ix]=(x1y1*(ix+1) + x2y1*(iy+1) + x3y1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号