Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@daikumatan

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

More than 5 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

1
Help us understand the problem. What is going on with this article?
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
daikumatan
2002-2015: Fujixerox, Numerical simulation Engineer 2015-2016: NVIDIA Japan, BD Manager 2016-2020: Rescale Japan, Evangelist 2020-Present: XTREME-D, CTO

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?