2014-05-25 459 views
2

我正在嘗試對矩陣矩陣乘法進行編碼,並且在幾次試驗後,每隔幾個試驗後它都會繼續給出分段錯誤。 我在網站上查找了不同的問題,並嘗試了以下兩種代碼的幾種方法。動態分配矩陣C進行乘法運算

此外,爲什麼我們需要指向「int指針指針」的指針,如int ** mat1,** mat2等?我不知道爲什麼要這樣做,但我在一些答案中看到了它。

代碼1

void mxmult() 
{ 
    int n,m,a,b,c,d, sum=0; 
    int x,y,z; 
    printf("Enter first order [n*n]\n"); 
    scanf("%d", &n); 
    printf("Enter second order [m*m]\n"); 
    scanf("%d", &m); 
    if (n!=m) 
    { 
     printf("Invalid orders"); 

    } 
    else 
    { 
     //mem allocate for matrix 1 
     int **mat1 = (int**)malloc(n*sizeof(int)); 
     for(x=0;x<n;x++) 
      { 
       mat1[x]=(int*)malloc(n*sizeof(int)); 
      } 
     // input matrix 1 
     printf("Enter the first matrix entries\n"); 
     for (a = 0; a <n; a++) 
     { 
      for (b = 0; b < n; b++) 
      { 
       scanf("%d", &mat1[a][b]); 
      } 
     } 
     // memory allocate matrix 2 
     int **mat2 = (int**)malloc(m*sizeof(int)); 
     for(y=0;y<n;y++) 
      { 
       mat2[y]=(int*)malloc(m*sizeof(int)); 
      } 

     //inpur matrix 2 
     printf("Enter the second matrix entries\n"); 
     for (c = 0; c <n; c++) 
     { 
      for (d= 0; d < n; d++) 
      { 
       scanf("%d", &mat2[c][d]); 
      } 
     } 

     //Memory allocate matrix Mult 
     int **mult=(int**)malloc(m*sizeof(int)); 
     for(z=0;z<m;z++) 
      mult[z]=(int*)malloc(m*sizeof(int)); 
     for (a = 0; a < n; a++) 
     { 
      for (d = 0; d < m; d++) 
      { 
       for (c = 0; c < n; c++) 
       { 
        sum=sum + (mat1[a][c] *mat2[c][d]); 
       } 
       mult[a][d] = sum; 
       sum= 0; 
      } 
     } 
     printf("Product\n"); 

     for (a = 0 ; a < n ; a++) 
     { 
      for (d = 0 ; d < m ; d++) 
       printf("%d\t", mult[a][d]); 
      printf("\n"); 
     } 

    } 
} 

代碼2:

void mxmult() 
{ 
    int n,m,a,b,c,d, sum=0; 
    int x,y,z; 
    printf("Enter first order [n*n]\n"); 
    scanf("%d", &n); 
    printf("Enter second order [m*m]\n"); 
    scanf("%d", &m); 
    if (n!=m) 
    { 
     printf("Invalid orders"); 

    } 
    else 
    { 
     //mem allocate for matrix 1 
     int **mat1 = (int**)malloc(n*n*sizeof(int)); 

     // input matrix 1 
     printf("Enter the first matrix entries\n"); 
     for (a = 0; a <n; a++) 
     { 
      for (b = 0; b < n; b++) 
      { 
       scanf("%d", &mat1[a][b]); 
      } 
     } 
     // memory allocate matrix 2 
     int **mat2 = (int**)malloc(m*m*sizeof(int)); 

      //input matrix 2 
     printf("Enter the second matrix entries\n"); 
     for (c = 0; c <n; c++) 
     { 
      for (d= 0; d < n; d++) 
      { 
       scanf("%d", &mat2[c][d]); 
      } 
     } 

     //Memory allocate matrix Mult 
     int **mult=(int**)malloc(m*m*sizeof(int)); 

      // Mx multiplicatn 
     for (a = 0; a < n; a++) 
     { 
      for (d = 0; d < m; d++) 
      { 
       for (c = 0; c < n; c++) 
       { 
        sum=sum + (mat1[a][c] *mat2[c][d]); 
       } 
       mult[a][d] = sum; 
       sum= 0; 
      } 
     } 
     printf("Product\n"); 

     for (a = 0 ; a < n ; a++) 
     { 
      for (d = 0 ; d < m ; d++) 
       printf("%d\t", mult[a][d]); 
      printf("\n"); 
     } 

    } 
} 

我一直在努力執行代碼2,然後,碼2。兩人都是在幾次戰鬥後纔給出賽格缺陷。

+0

不知道,但是這看起來不正確:'scanf函數( 「%d」,&MAT1 [A] [B] )'。因爲mat1已經是一個指針了,所以你不應該使用&符號。 – MightyPork

+1

'int ** mat1 =(int **)malloc(n * sizeof(int));' - >'int ** mat1 =(int **)malloc(n * sizeof(int *));' – BLUEPIXY

+0

int * mat1 =(int **)malloc(n * n * sizeof(int));' - >'int(* mat1)[n] = malloc(n * n * sizeof(int));'' – BLUEPIXY

回答

9

int **類型是什麼被稱爲衣衫襤褸的陣列。通過首先分配一個「脊柱」數組來創建一個不規則數組,該數組包含指向每個「肋骨」的指針。當您參考matrix[x][y]時,您在「脊椎」中取指針x處的指針,然後在「肋骨」中獲取索引「y」處的元素。下面是示出了該結構的漂亮的圖:

Ragged array in C

可以讀取comp.lang.c FAQ list · Question 6.16: How can I dynamically allocate a multidimensional array?獲得更多信息(也上述圖像的源)。

另一種選擇是實際爲您的矩陣分配一個二維數組(我的首選方法)。這要求編譯器支持一些C99結構,但除Microsoft C編譯器(例如gcc和clang)之外的所有主要編譯器似乎都默認支持這種編譯器。這裏有一個例子:

int (*matrix)[colCount] = (int(*)[colCount]) malloc(sizeof(int)*rowCount*colCount); 

上面的奇怪的語法是你如何declare a pointer to an array in C。需要圍繞*matrix的括號來從declaring from an array of pointers中消除歧義。 You don't need to cast the result of malloc in C,所以等效:

int (*matrix)[colCount] = malloc(sizeof(int)*rowCount*colCount); 

該分配的存儲器中的單個塊的矩陣,並且由於編譯器知道每行(即colCount)的長度,它可以插入數學計算用於任何適當的地址2D參考。例如,matrix[x][y]相當於((int*)matrix)[x*colCount+y]

我更喜歡分配一個二維數組,因爲您可以在一行中完成所有的分配,而對於不整齊的數組,您必須單獨設置指向每一行的指針,這通常需要另一條線路作爲循環。


至於你的內存設計缺陷,這一行看起來可疑:

int **mat1 = (int**)malloc(n*sizeof(int)); 

由於mat1是類型int**,在mat1每個條目應該是一個int*。但是,您的malloc正在使用sizeof(int)爲條目分配內存!試試這個:

int **mat1 = (int**)malloc(n*sizeof(int*)); 

假設你是一個64位系統上,sizeof(int)大概是4(字節),而sizeof(int*)應爲8(字節)。這意味着目前您正在分配內存的一半,因爲您需要,這意味着當您訪問該陣列的後半部分中的條目時會發生壞事。使用正確的尺寸(sizeof(int*))應該解決這個問題。

(可能還有其他的問題太多,但就是這樣乍一看站出來的人。)