具有指针标签结构的马洛克



我正在创建一个带有节点和边缘的简单图形。我得到了功能,但遇到了一些内存错误。

我在头文件中有一个 typedef 结构:

typedef struct Graph_s* Graph;

并在 c. 文件中实现:

struct Graph_s {
    Node* nodeArray;
    Edge* edgeArray;  
    size_t edges;
    size_t nodes;
};

和施工功能:

Graph create_graph() {
    Graph newGraph = malloc(sizeof(Graph)); 
    newGraph->edges = 0;
    newGraph->nodes = 0;
    return newGraph;
}

Graph newGraph = malloc(sizeof(Graph))给出的线路是:来自瓦尔格林德的Invalid write of size 8

malloc(sizeof(Graph))只为指针分配足够的内存。将其更改为 malloc(sizeof(struct Graph_s))

最新更新