簡體   English   中英

C.函數修改動態分配的2d數組時出現分段錯誤

[英]C. Segmentation Fault when function modifies dynamically allocated 2d array

我需要的是一個函數,可以修改給定的指向二維矩陣的指針,如下所示:

void intMatrixAll(int row, int col, int **matrix);

現在,一個函數應該分配內存,並且可以使用矩陣。 行和列在運行時給出。

#include <stdio.h>
#include <stdlib.h>
#define PRINTINT(X) printf("%d\n", X);
void intMatrixAll(int row, int col, int **matrix);

int main(void) {
   int testArrRow = 4;
   int testArrCol = 6;
   int **testMatrix = NULL;
   intMatrixAll(testArrRow, testArrCol, testMatrix);
   testMatrix[2][2] = 112; //sementation fault here :(
   PRINTINT(testMatrix[2][2]);
   system("PAUSE");
   return 0;
}

void intMatrixAll(int row, int col, int **matrix) {
   printf("intMatrixAll\n");
   //allocate pointers:
   matrix = malloc(row * sizeof(int *));
   if(matrix == NULL) printf("Failed to allocate memmory.\n");
   for(int i=0; i<row; i++) {
      //allocate space for cols: 
      matrix[i] = malloc(col * sizeof(int));
      if(matrix[i] == NULL) {
         printf("Failed to allocate memmory for arr[%d].\n", i);
         exit(0);
      }
   }
}

為什么我會出錯?

測試矩陣仍為NULL。 您需要從intMatrixAll()返回新分配的指針。 從函數返回值,或者傳入testMatrix的地址以便可以對其進行設置。

#include <stdio.h>
#include <stdlib.h>
#define PRINTINT(X) printf("%d\n", X);
void intMatrixAll(int row, int col, int **matrix);

int main(void) {
   int testArrRow = 4;
   int testArrCol = 6;
   int **testMatrix = NULL;
   intMatrixAll(testArrRow, testArrCol, &testMatrix);
   testMatrix[2][2] = 112; //sementation fault here :(
   PRINTINT(testMatrix[2][2]);
   system("PAUSE");
   return 0;
}

void intMatrixAll(int row, int col, int ***matrix) {
   printf("intMatrixAll\n");
   //allocate pointers:
   *matrix = malloc(row * sizeof(int *));
   if(*matrix == NULL) printf("Failed to allocate memmory.\n");
   for(int i=0; i<row; i++) {
      //allocate space for cols: 
      *matrix[i] = malloc(col * sizeof(int));
      if(*matrix[i] == NULL) {
         printf("Failed to allocate memmory for arr[%d].\n", i);
         exit(0);
      }
   }
}

因為修改intMatrixAll()內部的矩陣不會修改main()中的testMatrix。 如果您希望能夠修改main的變量,則需要傳遞一個指向它的指針。 因此,您需要將intMatrixAll更改為:

void intMatrixAll(int row, int col, int ***matrix)

在intMatrixAll內部,您現在需要將matrix更改為*matrix (並且在索引它時,您需要(*matrix)[...]

最后,您需要將對intMatrixAll的調用更改為:

intMatrixAll(testArrRow, testArrCol, &testMatrix);

之所以如此,是因為C僅支持按值傳遞,而按值傳遞不支持在調用方中更改變量值的被調用函數。

為了在調用方中修改變量的值,您需要將指針傳遞給該變量,然后使被調用函數將其取消引用。

在此處查看有關類似內容的討論。 seg錯誤的原因是因為您沒有將int的指針指向指針從intMatrixAll函數intMatrixAll到主例程。 換句話說,您正在通過而不是通過引用將雙指針的參數傳遞給int的參數,並試圖在實際上仍然為NULL嘗試訪問testMatrix

因此,您需要添加另一個間接級別,即*並將該間接級別用作將雙指針更改為malloc d的方法,並使主例程看到testMatrix確實已分配。

請參閱上方的R. Samuel Klatchko的答案。

通常,如果要通過引用將指向某物的指針作為參數傳遞,請添加另一種間接方式。 在您的情況下,您將雙指針傳遞給int,在函數中將參數設為三指針。

int **testMatrix;
void intMatrixAll(int row, int col, int ***matrix){
  // In here use this (*matrix)
}

// Then the calling of the function would look like this
intMatrixAll(testArrRow, testArrCol, &testMatrix); 

// Note the ampersand above to treat this double pointer passed in by reference

希望這對您有所幫助,湯姆。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM