2013-04-09 50 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

相关问题