2012-03-08 2 views
0

다음 코드는 큰 MAXV 값의 경우 insert_edge 함수 (~> next = g-> edges [x])에서 메모리 오류를 발생시킵니다. 작은 것들을 위해 그것은 잘 작동합니다. 문제가 어디에 있습니까? 어떻게 작동하는 구조체를 정의 할 수 있습니까?C 구조체의 메모리 오류

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

#define MAX_LINE_SIZE (1024*128) 


#define MAXV  23947347  /* maximum number of vertices */ 
#define NULLO    0  /* null pointer */ 


#define TRUE 1 
#define FALSE 0 

typedef int bool; 

typedef struct edgenode { 
    int y;    /* adjancency info */ 
    int weight;   /* edge weight, if any */ 
    struct edgenode *next;  /* next edge in list */ 
} edgenode; 


typedef struct { 
    edgenode *edges[MAXV+1]; /* adjacency info */ 
    int degree[MAXV+1];  /* outdegree of each vertex */ 
    int nvertices;   /* number of vertices in the graph */ 
    int nedges;   /* number of edges in the graph */ 
    int directed;   /* is the graph directed? */ 
} graph; 

initialize_graph(graph *g, bool directed) 
{ 
    int i;    /* counter */ 

    g -> nvertices = 0; 
    g -> nedges = 0; 
    g -> directed = directed; 

    for (i=1; i<=MAXV; i++) 
      g->degree[i] = 0; 
    for (i=1; i<=MAXV; i++) 
      g->edges[i] = NULL; 
} 

read_graph(graph *g, bool directed, const char *filename) 
{ 

    FILE *f; 
    char line[MAX_LINE_SIZE], buf[10]; 
    int format, rc; 
    int edge; 
    int vertex_n; 
    int vertex_m; 

    char *token,*token2, *s; 

    int v; 

    int i;    /* counter */ 

    /* open file */ 
    f = fopen (filename, "r"); 
    if (f == NULL) 
     return NULL; 


    rc = sscanf (line, "%d %d %d", &(vertex_n), &(vertex_m), &(edge)); 

    initialize_graph(g, directed); 

    for (i=1; i<=edge; i++) { 
     s = fgets (line, MAX_LINE_SIZE, f); 

     token = strtok (line, " "); 

     token2 = strtok (NULL, " "); 

     int s = atoi(token); 
     int t = atoi(token2); 

     printf("%d, %d\n", start, ziel); 

     insert_edge(g,s,t,directed); 

} 

} 

insert_edge(graph *g, int x, int y, bool directed) 
{ 
    edgenode *p;   /* temporary pointer */ 

    p = malloc(sizeof(edgenode)); /* allocate storage for edgenode */ 

    p->weight = NULL; 
    p->y = y; 
    p->next = g->edges[x]; 
     g->edges[x]; 

    g->edges[x] = p;  /* insert at head of list */ 

    g->degree[x] ++; 

    if (directed == FALSE) 
     insert_edge(g,y,x,TRUE); 
    else 
     g->nedges ++; 
} 

    main() 
    { 
     graph g; 

     read_graph(&g,FALSE, "/path/graph_with_23947347_nodes.mtx");// 
    } 
+0

K & R C? ANSI로 전환하는 것이 좋습니다. –

+0

"큰 MAXV"값의 크기는 어느 정도입니까? – Jay

+0

매개 변수 x가 범위를 벗어 났는지 확인하십시오 (MAXV) – ydroneaud

답변

2
graph g; 

가 결정적으로 스택에 맞게 너무 큽니다. 대신 힙에 넣으십시오.

graph* g=malloc(sizeof(graph)); 

그레이엄의 조언도 따르십시오. 힙의 경우에도 너무 클 수 있습니다.

+0

나는 충분한 기억이있다;) 그러나 이것은 문제이었다. 고맙습니다! – ItsameMario

1

malloc의 반품 값을 확인 하시겠습니까?

p = malloc(sizeof(edgenode)); /* allocate storage for edgenode */ 

단순히 메모리가 부족할 수 있습니다. 계속 진행하기 전에 pNULL이 아닌지 확인하십시오.

0
p->next = g->edges[x]; 
     g->edges[x]; 

    g->edges[x] = p;  /* insert at head of list */ 

이 코드는 의미가 없습니다.

단일 링크 된 목록에서 전에 노드 앞에 기존 노드에 대한 포인터가없는 기존 노드를 삽입 할 수 없습니다. 그렇지 않으면 이전 노드가 어떻게 다음 포인터를 업데이트 할 수 있습니까?

이것은 후 기존 노드를 노드 를 삽입 :

p->next = g->edges[x]->next; 
g->edges[x]->next = p; 
2

스택 오버 플로우가있는 것 같습니다. 은 큰 데이터 구조이며 main에 로컬로 (스택에) 할당하고 있습니다. 메인을 다음으로 변경해보세요.

int main(void) 
{ 
    graph *g = malloc(sizeof(graph)); 
    if (g != NULL) 
    { 
     read_graph(g, FALSE, "/path/graph_with_23947347_nodes.mtx"); 
     free(g); 
    } 
    return 0; 
} 
관련 문제