小编Wee*_*han的帖子

当我将图中的节点数从4增加到大于5时,malloc受到内存破坏

我编写了一个简单的代码来打印给定边的图形,并且我正在使用结构将数据存储在图形中,当节点数少于5个但大于5个会带来malloc错误时,它可以很好地工作。不知道为什么会这样,有人可以指出我做错了什么。

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

// Define maximum number of vertices in the graph

#define N 5
//#define N 4 //use this when the nodes are less than 5 

// Data structure to store graph
struct Graph {
    // An array of pointers to Node to represent adjacency list
    struct Node *head[N];
};

// A data structure to store adjacency list nodes of the graph
struct Node {
    int dest;
    struct Node *next;
};

// data structure to …
Run Code Online (Sandbox Code Playgroud)

c malloc struct graph

1
推荐指数
1
解决办法
57
查看次数

标签 统计

c ×1

graph ×1

malloc ×1

struct ×1