2012-07-30 162 views
0

我有一個分段錯誤,想知道我的錯誤在哪裏。二維數組上的指針

讓我解釋一下。

在我的主,我宣佈一個三維數組:int*** Matricegroupegeneralisant

那麼這主要使用功能recuperationinfoFich(&matricegroupegeneralisant); 此函數聲明爲:recuperationinfoFich(int* * * * matricegroupegeneralisant)

此功能recuperationinfoFich使用另一個功能recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[Ni]); 此函數聲明as recuperationmatricegroupesgeneralisants(int*** matricegroupegeneralisant)

我的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

void allocationdynamiquetableautroisdimdentier(int**** Matrice,int nbniveau, int nbligne, int nbcolonne) 
{ 
int i,j; 
    *Matrice=(int***) malloc (sizeof(int**)*nbniveau); 
    for (i=0; i<nbniveau; i++) 
    { 
     (*(Matrice))[i]=(int**) malloc (sizeof(int*)*nbligne); // allocation dynamique de la matrice Matrice 
     for (j=0; j<nbligne; j++) 
     { 
      ((*(Matrice))[i])[j]=(int*) malloc (sizeof(int)*nbcolonne); 
     } 
    } 
} 


void recuperationmatricegroupesgeneralisants(int*** matricegroupegeneralisantA) 
{ 
    (*matricegroupegeneralisantA)[0][1]=1; 
} 

void recuperationinfoFich(int**** matricegroupegeneralisantA) 
{ 
    allocationdynamiquetableautroisdimdentier(matricegroupegeneralisantA,3, 3, 7); 
    recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[1]); 
} 



void main(int args, char **argv) 
{ 

    int*** matricegroupegeneralisantA; 

    recuperationinfoFich(&matricegroupegeneralisantA); 
} 

使用gdb:

(gdb) r 
Starting program: /home/larimsna1/Desktop/a.out 

Breakpoint 1, 0x000000000040061a in main() 
(gdb) n 
Single stepping until exit from function main, 
which has no line number information. 

Program received signal SIGSEGV, Segmentation fault. 
0x00000000004005c8 in recuperationmatricegroupesgeneralisants() 
(gdb) 
+0

您對調試做了什麼?你有沒有找到這個分部門?我會使用gdb或類似的方式來定位段落代碼 – dtech 2012-07-30 18:07:56

+0

當遇到段落錯誤或任何其他類型的崩潰時,您應該做的第一件事就是使用調試器。它將幫助您找到發生崩潰的位置,並讓您檢查變量以查看可能導致崩潰的原因。 – 2012-07-30 18:08:18

+0

此外,在正確調用'recuperationinfoFich'和'allocationdynamiquetableautroisdimdentier'時,您不會正確調用'recuperationmatricegroupesgeneralisants'。 _或在'recuperationmatricegroupesgeneralisants'中正確使用傳入的參數。 – 2012-07-30 18:11:19

回答

1

我懷疑問題有事情做與你的分配功能。這就是說,你提供的代碼有許多功能和風格問題。在一個地方,你沒有足夠的時間去引用一個指針,導致你將一個整數指定給一個指針類型。當你不能傳遞參數並使用返回值時,你不必要地使用大部分事務的指針。

此代碼應該做你想要什麼,並應能正常工作,並且更容易閱讀:

// allocation, returns pointer to allocated value 
int *** allocationdynamique(int nbniveau, int nbligne, int nbcolonne) 
{ 
    int *** Matrice; 
    int i, j; 
    Matrice = (int ***) malloc (sizeof(*Matrice) * nbniveau); 
    for (i = 0; i < nbniveau; ++i) 
    { 
     Matrice[i] = (int **) malloc (sizeof(**Matrice) * nbligne); 
     for (j = 0; j < nbligne; ++j) 
     { 
      Matrice[i][j] = (int *) malloc (sizeof(***Matrice) * nbcolonne); 
     } 
    } 
    return Matrice; 
} 

// computation, takes 2D array, modifies in place 
void recuperationmatrice(int** matrice) 
{ 
    matrice[0][1] = 1; 
} 

// you shouldn't use void main, its not part of the standard 
int main(int args, char **argv) 
{ 
    int*** matrice = allocationdynamique(3, 3, 7); 
    recuperationmatrice(matrice[1]); 

    return 0; 
} 

另外,從風格的角度來看,你的變量名是可笑的長,你應該圍繞使用空格運營商。

此代碼中的許多錯誤都是編譯器能夠檢測並警告您的錯誤。它們是有效的C代碼,可以正確編譯,但在這個和大多數其他情況下寫入錯誤,並且不會按預期工作。您應編譯編譯器警告以清除可能發生的事故:

gcc -Wall -c file.c -o file.o 
+1

只是爲了增加關於風格問題的觀點,如果你有一個以上的單詞的變量或函數名,最好是[camel case](http://en.wikipedia.org/wiki/CamelCase)它或添加下劃線。 例如將'allocationdynamique'改爲'allocationDynamique'或'allocation_dynamique'。 – 2012-07-30 18:53:04

+1

還值得注意的是,在我見過的所有命名約定中,大寫變量名稱和所有小寫函數名稱不帶下劃線不是其中之一。總的來說,即使你自己創作,你使用哪種風格也沒有任何意義,但我不能強調一致性的重要性。閱讀沒有特定風格的代碼對你來說很難,對其他人來說也很難。選擇一種風格並堅持下去。戈登也是忍者。 – Wug 2012-07-30 18:54:32