2013-04-09 67 views
0

這裏是我的問題:我有一個二維矩陣焦炭至極我用的malloc函數。後來,我想從一個文件中的地圖,但是我有一個分段錯誤在那裏,我不知道爲什麼... 下面是一個代碼示例:ç - 的malloc或與fgets,我不知道發生了什麼不工作

// struct where I put the map and others informations from the 
typedef struct problem_t 
{ 
    char *nom; 
    Coordonnees arrivee, depart; 
    int nb_ligne, nb_col; 
    char **  
} Problem; 

// Function wich malloc the map 
int mallocCarte(char *** carte, int nbLigne, int nbCol) 
{ 
    *carte = malloc(nbLigne * sizeof(char*)); 

    if (*carte == NULL) 
    { 
     return false; 
    } 

    int i; 
    for (i = 0; i < nbLigne ; ++i) 
    { 

     (*carte) [i] = malloc(nbCol * sizeof(char)); 
     if ((*carte) [i] == NULL) 
     { 
      return false; 
     } 
    } 

    return true; 

} // mallocCarte () 

// Code sample, I've already got the others informations, now, I'd like to get the map 
// On commence par reserver l'espace memoire reserve à la carte. 
int res = mallocCarte(&problem->carte, problem->nb_ligne, problem->nb_col); 

// Si l'allocation s'est mal passée, on renvoie un message 
if (res == false) 
{ 
    exc.num_Exc = MALLOC_ERROR; 
    exc.msg_Err = "Erreur lors de l'allocation mémoire de la carte"; 
    return exc; 
} 

printf("Recuperation de la carte 2 ...%d %d\n", problem->nb_ligne, 
     problem->nb_col); 
int nbLi = 0; 
int nbCol = 0; 
while (fgets(fromFile, 1, file) != NULL && nbLi < problem->nb_ligne) 
{ 
    if (fromFile [0] == '\n') 
    { 
     nbCol = 0; 
     ++nbLi; 
     continue; 
    } 

    if (nbCol == problem->nb_col) 
    { 
     printf("malformed input file!\n"); 
     exit(-1); 
    } 


    (problem->carte) [nbLi] [nbCol++] = fromFile [0]; 
} 

它已經很多天,我真的不知道該怎麼辦... 我會如此偉大如果有人能幫助我!

感謝您

(這裏是我拿信息的源文件。首先,他們是問題的名字,然後一些座標,最後是地圖的大小。在文件的結尾是地圖 https://dl.dropbox.com/u/56951442/map.txt

+4

這是否甚至編譯?..我有一些關於'struct'聲明的char **'行的疑問。 – dasblinkenlight 2013-04-09 17:08:32

+1

您不需要在C程序中投射'malloc'的返回值。 'sizeof(char)'是'1'。 – 2013-04-09 17:09:13

+0

如果你想通過out參數返回一個2d數組,參數需要'char ***'類型。然後你的第一個任務就像'carte = malloc(nbLigne * sizeof(char *));' – jpm 2013-04-09 17:11:45

回答

1

當你遞增mainnbLi,你需要重置nbCol爲零。您允許列繼續無限增加並超出陣列的大小。

此外,you should not cast malloc in C

此外,你應該通過& problem->點到你的配置功能...

// Function wich malloc the map 
int mallocCarte(char *** carte, int nbLigne, int nbCol) 
{ 
    *carte = malloc(nbLigne * sizeof(char*)); 
... 
    (*carte)[i] = malloc(nbCol * sizeof(char)); 
... 
} 

main() 
{ 
    ... 
    int res = mallocCarte(&problem->carte, problem->nb_ligne, problem->nb_col); 
    ... 
} 

你或許應該添加一個測試,也保證你不要跑題結束列,如果你輸入的文件格式不正確......

if (isspace(fromFile [0])) 
    { 
     nbCol = 0; 
     ++nbLi; 
     continue; 
    } 

    if(nbCol == problem->nb_col) 
    { 
     printf("malformed input file!\n"); 
     exit(-1); 
    } 

你確定你的意思是使用fgets(fromFile, 1, file)?以下說明暗示fgets()總是返回一個空字符串,直到文件結尾,並且fromFile[0]將始終爲'\0',直到EOF。您應該使用fgets(fromFile, 2, file)

與fgets()讀取最多一個小於字符大小從流 並將它們存儲到緩存由s指向。讀取後 EOF或換行符停止。如果讀取換行符,則將其存儲到 緩衝區中。 '\ 0'存儲在緩衝區中的最後一個字符之後。

+0

好吧,它也沒有工作 – Carvallegro 2013-04-09 17:16:44

+0

我把所有的建議放在我的代碼中,但它仍然不起作用: /另外,你能解釋*** char和* carte的東西嗎?我真的不知道 – Carvallegro 2013-04-09 17:24:15

+0

你正在分配一個char **,然後你爲char **的每個元素分配一個char * - 但是,你想要將新分配的指針返回給調用者。這意味着你必須將新的指針寫入該參數並讓調用者稍後閱讀......想想這樣......如果將一個整數傳遞給一個函數,那麼函數可以讀取並更改該參數它想要,但調用者不會看到這些更改,因爲您傳遞了整數的COPY。如果您將POINTER傳遞給整數,然後修改其內容,則調用者將看到更改。 – 2013-04-09 17:30:29

-1

問題是結構problem_t的對象。 它不是一個指針,你應該訪問結構變量像

problem.carte, 
problem.nb_ligne 
problem.nb_col 

我想我的系統上的代碼。 你的代碼對於我的map.txt工作正常。我剛從文件中聲明char [2]; while(fgets(fromFile,2,file)!= NULL){//打印文件}。它的打印文件適合我。

+0

不,在代碼中,問題是problem_t *參數 – Carvallegro 2013-04-09 17:42:50

+0

如果是這樣的話,代碼將不能編譯......這是一個給定的問題是通過代碼設計指向problem_t。 – 2013-04-09 17:49:37

+0

你的代碼對於我的map.txt工作正常。我剛從文件中聲明char [2]; while(fgets(fromFile,2,file)!= NULL){//打印文件}。它的打印文件適合我。 – surender8388 2013-04-09 19:49:10

相關問題