0
텍스트 파일에서 읽고 구조를 동적으로 만들려는 프로그램을 작성하고 있습니다.런타임시 힙 손상을 해결할 수 없습니다.
그래서 나는 그것을 할 수있는 다음과 같은 코드가 있습니다
typedef struct tgraph {
int vertex_count;
int edge_count;
struct tvertex **adj;
} Graph;
typedef struct tvertex {
int n; /* vertex number */
char course[255];
struct tvertex *next;
} Vertex;
Graph g_graph;
void create_graph(FILE *fd)
{
int vertex_count;
int edge_count;
fscanf(fd, "%i", &vertex_count);
fscanf(fd, "%i", &edge_count);
printf("Vertices: %i\n", vertex_count);
printf("Edges: %i\n", edge_count);
g_graph.vertex_count = vertex_count;
g_graph.edge_count = edge_count;
g_graph.adj = malloc(sizeof(Vertex *));
Vertex **vlist = g_graph.adj;
int i;
for (i = 0; i < vertex_count; i++) {
Vertex *vertex = malloc(sizeof(Vertex));
fscanf(fd, "%i,%[^\n]", &vertex->n, vertex->course);
printf("%i %s\n", vertex->n, vertex->course);
*vlist = vertex;
vlist ++;;
}
}
나는이 기능 create_graph
를 호출하고 런타임에이 오류를 받고 있어요을
손상 이중 연결리스트 : 0x00000000007d7240
이것은 주 기능에서 fclose(fd)
을 호출하여 발생합니다. 문제는 내가 힙을 손상시키고 있다는 것입니다. 포인터 연산이 잘못되었지만 해결할 수 없습니다.
저는 리눅스에서 gcc로 컴파일 중입니다.
멋졌습니다. 고마워, 나는 그런 꼬리를 짓고 있었다. 그러나 나는 확실하지 않았다. 그리고 그것은 나에게 명백하지 않았다. 당신의 대답이 나를 알아 냈습니다. – kaneda