2015-12-21 143 views
1

我在构建一个用图形做各种事情的程序时遇到了这个问题。 我使用代码块在C中编写代码,如果我正常运行它(通过“构建和运行”)程序“工作”(它仍然没有完成),但如果我尝试通过调试器运行代码,它会停止与Segmentation fault。这很奇怪。代码正常运行,但调试给出了“分段错误”

下面是我的一些代码提取为了解决这个问题,遗憾的混乱:

的main.c

FILE *file; 
char *input = "input.txt"; 
file = fopen(input, "r"); // Open the file read-only 
if(file != NULL){ 
    G = parse(file, F);  // Parse the graph 
fclose(file); // Close the file 

Graph.h

struct TGraph { 
    void **adj; 
    int nodes_count; 
}; 

typedef struct TGraph *Graph; 
typedef enum GraphType {LIST, MATRIX} GraphType; 

typedef Graph (*INITGRAPH)(int); 
typedef void (*ADDADJ)(Graph, int, int, float); 
typedef void (*PRINT)(Graph); 

typedef struct funct{ 
    INITGRAPH init; 
    ADDADJ addEdge; 
    PRINT print; 
}FunctGraph; 

typedef struct AdjList{ //I need this in order to use the adj as a List 
    List *nodes; 
}AdjList; 

Graph.c

Graph initGraphList(int nodes_count){ 
    Graph G = malloc(sizeof(struct TGraph)); 
    ((AdjList *)(G->adj))->nodes = malloc(nodes_count * sizeof(List)); <<< PROBLEM HERE 
    G->nodes_count = nodes_count; 
    return G; 
} 

Gra phparser.c

Graph parse(FILE *file, FunctGraph *F){ 
    Graph G = NULL; 
    puts("Controllo numero di nodi..."); 
    if (!match(file, LPAR)){  // Check if number of nodes is present 
     syntax_error(errorsymb(LPAR), file); 
    }else{ 
     fseek(file,1,SEEK_CUR);  //Going over the LPAR 
     G = parse_init(file, F->init); //Initialize the Graph <<< PROBLEM HERE 
     if (G == NULL){ 
      fprintf(stderr, "Errore nell'allocazione del grafo\n"); 
      exit(1); 
     }else{ 
      if (!match(file, RPAR)) 
       syntax_error(errorsymb(RPAR), file); 
      else{     // If so parse the graph 
       fseek(file,1,SEEK_CUR); 
       printf("Rilevato grafo da %d nodi\n", G->nodes_count); 
       puts("Costruisco il grafo..."); 
       while(!match(file, DOT)){ 
        read_node(G, file, F->addEdge); 
       } 
      } 
     } 
    } 
    return G; // return the parsed Graph 
} 

int match(FILE *file, type et) 
{ 
    // Try to match symbol of expected type 'et' in file 
    // [returns 1 if successful, 0 otherwise] 
    char c; 
    type rp;      // Type of the symbol read 
    int res=0; 
    while(((c = fgetc(file)) == '\t') || (c == '\n') || (c == ' ')); // Skip intitial tabulation, newline and spaces 
    switch(c) {  // Determine the read symbol type 
     case ',': 
      rp = COMMA; break; 
     case '(': 
      rp = LPAR; break; 
     case ')': 
      rp = RPAR; break; 
     case '.': 
      rp = DOT; break; 
     case '-': 
      rp = MINUS; break; 
     case '>': 
      rp = ARROW; break; 
     case ';': 
      rp = SEMICOL; break; 
     default : 
      rp = NODEID; break; 
    } 
    ungetc(c,file); // Push the characters read back to the file 
    if (rp==et)  // The expexted type et and the read symbol type rp match 
     res = 1; 
    return res; 
} 

Graph parse_init(FILE *file, INITGRAPH init){ 
    unsigned int nodes; 
    fscanf(file, "%d", &nodes); 
    if(nodes >= INT_MAX) 
     syntax_error("Numero nodi troppo grande", file); 
    return init(nodes); // Initialize the graph 
} 

我认为这应该是我们需要找到这个问题的所有代码。 作为我的标记代码中的问题奠定了此行中:

((AdjList *)(G->adj))->nodes = malloc(nodes_count * sizeof(List)); 

这里的调试器调用“段错误”,但我不明白为什么。 你有没有任何想法,为什么代码运行没有调试器,而不是它,给出了这个错误? 在此先感谢您的帮助。

回答

2
Graph G = malloc(sizeof(struct TGraph)); 
((AdjList *)(G->adj))->nodes = malloc(nodes_count * sizeof(List)); 

您要为G分配与malloc内存,但你永远不与有效值initlize,所以提领G->adj会导致不确定的行为。它似乎在发行版中起作用,但在调试版本中不起作用。

如果你正在使用Visual Studio,调试版本会初始化内存(我认为0xFE什么的,所以它会在你取消引用指针时崩溃,在发布版本中这不会发生,所以它有一些随机。地址,这显然不会造成任何直接的问题,不知道是否也GCC做到这一点

你需要的是这样的:

Graph G = malloc(sizeof(struct TGraph)); 
G->adj = malloc(whatever); 
G->node_count = 0; 
etc. 
((AdjList *)(G->adj))->nodes = malloc(nodes_count * sizeof(List)); 
+0

我试图用你的建议分配是这样的:“G->因为想在稍后将其转换为'AdjList *' 调试器不会调用在那里停下来,但是当试图访问列表的元素时,我收到另一个“分段错误”。 我认为我的分配仍然存在问题。 – Aster

+0

@Baum 问题是我必须创建一个Graph结构,它可以支持列表和邻接矩阵,为了做到这一点,老师教我们使用'void *'指针,以便将该元素与任何一种方法。 有没有办法确保'G-> adj'是否指向一个有效的'AdjList'(或'AdjMatrix')对象? 当然,感谢你们双方在你身边帮助我。 – Aster

+0

你必须分配足够的空间,以便为结构中的较大结构保留所需数量的元素。为了使它更易于维护,联合可能是一个好主意,因为那样你可以根据需要在任何元素上添加元素而不需要投射它,而且sizeof也会给你正确的尺寸。 – Devolus

相关问题