繁体   English   中英

带指针标签结构的Malloc

[英]Malloc with pointer tag structure

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

我在头文件中有一个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))给出:Valgrind Invalid write of size 8

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM