Help us understand the problem. What is going on with this article?

Cで負のIndexが使える2次元配列を動的にかつ連続に確保する

More than 3 years have passed since last update.

物理シミュレーションをやってますが、配列で負のIndexが使いたくなってやってみました。
以外とさくっとできた。

下記のような使い方をイメージして書いてます

2次元配列、myArray[j][i]のi, jは

imin <= i < imax
jmin <= j < imax

imin < 0
jmin < 0

imax >= 0
jmax >= 0

メモリアクセスが連続となる方をiとしています。i, jの順番に注意してください。

    #include <iostream>

    int **allocateNegativeIndex2DimArray(
        int jmin,       /* array[jmin][] jmin  < 0 */
        int jmax,       /* array[jmax][] jmax >= 0 */
        int imin,       /* array[][imin] imin  < 0 */
        int imax        /* array[][imax] imax >= 0 */
    )
    {

        const int ni = imax - imin;
        const int nj = jmax - jmin;
        const int numTotal = ni * nj;

        int **tmp = (int **)malloc(nj * sizeof(int));
        tmp[0] = (int *)malloc(numTotal * sizeof(int));

        for (int j = 1; j < nj; j++) {
            tmp[j] = tmp[j - 1] + ni;
        }

        const int icenter = abs(imin);
        const int jcenter = abs(jmin);

        int **val = &tmp[jcenter];
        for (int j = jmin; j < jmax; j++) {
            val[j] = &tmp[j + jcenter][icenter] + ni;
        }

        return val;
    }

    int main() {

        int xmin = -2;
        int xmax =  3;
        int ymin = -1;
        int ymax =  2;

        int **myArray = allocateNegativeIndex2DimArray(ymin, ymax, xmin, xmax);

        /*
         * 実際に負のインデックスが入るか表示テストする
         */
        for (int j = ymin; j < ymax; j++){
            for (int i = xmin; i < xmax; i++) {
                myArray[j][i] = i * j;
                printf("myArray[%2d][%2d] = %3d\n", j, i, myArray[j][i]);
            }
        }
        return 0;
    }
myArray[-1][-2] =   2
myArray[-1][-1] =   1
myArray[-1][ 0] =   0
myArray[-1][ 1] =  -1
myArray[-1][ 2] =  -2
myArray[ 0][-2] =   0
myArray[ 0][-1] =   0
myArray[ 0][ 0] =   0
myArray[ 0][ 1] =   0
myArray[ 0][ 2] =   0
myArray[ 1][-2] =  -2
myArray[ 1][-1] =  -1
myArray[ 1][ 0] =   0
myArray[ 1][ 1] =   1
myArray[ 1][ 2] =   2

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした