鍍金池/ 問答/C  C++/ load of null pointer of type 'int'

load of null pointer of type 'int'

leetcode上的463題目

int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
    if(grid == NULL || gridRowSize <= 0 || gridColSize <= 0) 
        return 0;
    grid = (int **) malloc ( gridRowSize * sizeof(int *));
//1土地  0 水
    int i = 0, j = 0;
    int sum = 0;
    int count = 4;
    for(i; i < gridRowSize; i++) {
        for(j; j < gridColSize; j++) {
            if( grid[i][j] == 0)
                continue;

            else {
                if( grid[i-1][j] == 1 && (i-1) >= 0) {   //如果該土地上面為土地
                    count --;
                }

                if( grid[i+1][j] == 1 && (i+1) <= gridRowSize-1) {   //如果該土地下面面為土地
                    count --;
                }

                if( grid[i][j-1] == 1 && (j-1) >= 0) {   //如果該土地左面為土地
                    count --;
                }

                if( grid[i][j+1] == 1 && (j+1) <= gridColSize-1) {   //如果該土地右面為土地
                    count --;
                }
                sum += count;
                count = 4;
            }
        }
    }

    return sum;
}

報錯是"Line 11: load of null pointer of type 'int'"
也就是這一行代碼
if(gridi == 0) continue;
請問這是什么原因?

回答
編輯回答
淺時光

grid[i] 是個int* 不錯,但是你的grid[i]指向哪里了你?你沒有為它分配內存。

函數(shù)傳入的grid有什么意義呢?

2017年3月9日 16:49