在 C 中使用邻接表初始化基于数组的图时出现问题
我以前玩过 Graphs,并且在 StackOverflow 的一些帮助下很好地管理了它,但我从未使用过像下面这样的结构。我似乎无法理解我在这里做错了什么...
#include "stdio.h"
#include "stdlib.h"
#define MaxV 100
#define MaxE 50
typedef struct edge {
int dest;
int cost;
struct edge *next;
} Edge, *Graph[MaxV];
Graph *initGraph() {
Graph *g = (Graph*)malloc(sizeof(Edge) * MaxV);
for(int i = 0; i < MaxV; i++)
(*g[i])->next = NULL;
return g;
}
int main(void) {
Graph *g = initGraph();
for(int i = 0; i < MaxV; i++) {
if((*g[i])->next == NULL) printf("[%02d] NULL\n", i);
}
return 0;
}
我在 (*g[i])->next = NULL;
的第一次迭代中遇到分段错误不明白为什么。我已经尝试了无数的方法,但我似乎无法使用这种结构来管理图形初始化。另外,我声明和返回指向 Graph 的指针的方式对于该结构是否正确?
我是否在 init 函数中使用大量指针使事情变得复杂还是什么?
PS:请不要提出不同的结构定义,我无法更改上面的任何内容。这才是真正的问题。我知道如何使用图表滚动我自己的结构,但我需要使用上面的。
I've played with Graphs before and I managed it alright with some help from StackOverflow but I never used a structure like the one below. I can't seem to understand what I'm doing wrong here...
#include "stdio.h"
#include "stdlib.h"
#define MaxV 100
#define MaxE 50
typedef struct edge {
int dest;
int cost;
struct edge *next;
} Edge, *Graph[MaxV];
Graph *initGraph() {
Graph *g = (Graph*)malloc(sizeof(Edge) * MaxV);
for(int i = 0; i < MaxV; i++)
(*g[i])->next = NULL;
return g;
}
int main(void) {
Graph *g = initGraph();
for(int i = 0; i < MaxV; i++) {
if((*g[i])->next == NULL) printf("[%02d] NULL\n", i);
}
return 0;
}
I get a segmentation fault on the first iteration of (*g[i])->next = NULL;
and I can't understand why. I've tried countless things but I can't seem to manage the Graph initialization with such structure. Also, is the way I'm declaring and returning a pointer to a Graph done the right way for this structure?
Am I complicating things with lots of pointers in the init function or what?
P.S: Please do not suggest different structure definitions, I cannot changing anything in the one above. That's the real issue. I know how to work with Graphs rolling my own structure, but I need to use the one above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不太明白你的第二个 typedef *Graph[MaxV]。
我要做的就是声明另一个结构,如下所示:
然后您可以按如下方式初始化图表:
打印出图表如下:
我想您会发现为图表添加一个额外的结构将证明更具可持续性时间也。 :)
I don't really understand your second typedef of
*Graph[MaxV]
.What I would do is declare another struct as follows:
Then you can initialize the graph as follows:
And printing out the graph is as follows:
I think you'll find that having an extra struct for the graph will prove to be more sustainable over time too. :)
鉴于OP要求不以任何方式更改typedef,对答案进行了完整修订。
我建议改为:
这里的问题是“指针数组”综合症。即
Graph** g
相当于Graph *g[10]
,但明显的条件是外部数组大小是固定的。这会产生问题,因为您无法返回固定大小的数组,但您可以返回**
。我仍然不确定图表数组的用例是什么,但是哎呀,这通过了 valgrind。
Complete revision of answer given the OP's requirement not to change the typedef in any way.
I would advise changing to this:
The problem here is the "array of pointers" syndrome. Namely
Graph** g
is equivalent toGraph *g[10]
with the obvious proviso that the outer array size is fixed. That creates problems because you can't return a fixed size array, but you can return a**
.I'm still not sure what the use case for an array of graphs is, but heck, this passes valgrind.
我想我找到了我正在寻找的解决方案。我尽可能地使用 Valgrind 来测试读/写权限,并且没有错误。是的,存在内存泄漏,但这不是这个问题的重点(我知道这些,不用担心)。
这是创建简单图表的完整代码。很想听听这个实现可能存在的任何问题......
I think I found the solution I was looking for. I used Valgrind as best as I could to test for read/write permissions and there were no errors. Yes there were memory leaks, but that's not the point of this question (I'm aware of those, don't worry).
Here's the whole code to create a simple graph. Would love to hear any problems this implementation might have...