用C语言创建具有邻接矩阵的图
我在用C语言实现一个基于邻接矩阵的图形程序。 但是当我初始化矩阵(赋值为零)时,我得到了一个分段错误。 我不确定我是否在访问双指针时出错 有人能帮我解决这个问题吗 代码如下:用C语言创建具有邻接矩阵的图,c,struct,segmentation-fault,malloc,C,Struct,Segmentation Fault,Malloc,我在用C语言实现一个基于邻接矩阵的图形程序。 但是当我初始化矩阵(赋值为零)时,我得到了一个分段错误。 我不确定我是否在访问双指针时出错 有人能帮我解决这个问题吗 代码如下: struct Graph { int V; int E; int **adj; }; struct Graph *addelements() { int i,j,a,u,v; struct Graph *G= (struct Graph*)malloc(sizeof(str
struct Graph {
int V;
int E;
int **adj;
};
struct Graph *addelements() {
int i,j,a,u,v;
struct Graph *G= (struct Graph*)malloc(sizeof(struct Graph*));
printf("Enter the number of vertices and edges : ");
scanf("%d %d", &G->V,&G->E);;
printf("%d, %d\n",G->V ,G->E);
//G->adj = (int **)malloc(sizeof(int **)*(G->V * G->E));
G->adj = malloc(sizeof(G->V * G->E));
//Initialization of vertices
for(i=0;i<=G->V;i++) {
for(j=0;i<=G->V;j++) {
G->adj[i][j]=0;
}
}
//Reading the edges;
for(i=0;i<G->E;i++) {
printf("Enter the source and destination : ");
scanf("%d %d\n", &u,&v);
G->adj[u][v]=1;
G->adj[v][u]=1;
}
//printing the matrix
for(i=0;i< G->V;i++) {
for(j=0;i< G->V;j++) {
printf("%d", G->adj[i][j]);4
}
}
return G;
}
int main() {
struct Graph *a= (struct Graph*)malloc(sizeof(struct Graph*));
a = addelements();
}
结构图{
INTV;
INTE;
int**adj;
};
结构图*加法器(){
int i,j,a,u,v;
结构图*G=(结构图*)malloc(sizeof(结构图*);
printf(“输入顶点和边的数量:”);
scanf(“%d%d”,&G->V,&G->E);;
printf(“%d,%d\n”,G->V,G->E);
//G->adj=(int**)malloc(sizeof(int**)*(G->V*G->E));
G->adj=malloc(sizeof(G->V*G->E));
//顶点初始化
对于(i=0;iV;i++){
对于(j=0;iV;j++){
G->adj[i][j]=0;
}
}
//阅读边缘;
对于(i=0;即;i++){
printf(“输入源和目标:”);
scanf(“%d%d\n”、&u和&v);
G->adj[u][v]=1;
G->adj[v][u]=1;
}
//打印矩阵
对于(i=0;i您正在
malloc
为指针填充空间。改用:
malloc(sizeof(struct Graph));
您正在
malloc
为指针填充空间。改用:
malloc(sizeof(struct Graph));
我认为你需要修改
struct Graph *G= (struct Graph*)malloc(sizeof(struct Graph*));
到
因为您需要分配空间来存储结构变量,而不是指针
而且您没有为G->adj
正确分配空间
试一试
换行符在末尾。看
此外,您还需要检查输入的u
和v
值是否在中的限制范围内
for(i=0;i<G->E;i++)
{
printf("Enter the source and destination : ");
scanf("%d %d", &u,&v);
if(u<G->V && v<G->V)
{
G->adj[u][v]=1;
G->adj[v][u]=1;
}
else
{
printf("\ninvalid value. Try again.");
i--;
}
}
请注意,如果分配内存失败,malloc()
将返回NULL
。您需要检查malloc()
调用是否也失败
您不必在C.Read中强制转换malloc()的返回值。我认为您需要修改
struct Graph *G= (struct Graph*)malloc(sizeof(struct Graph*));
到
因为您需要分配空间来存储结构变量,而不是指针
而且您没有为G->adj
正确分配空间
试一试
换行符在末尾。看
此外,您还需要检查输入的u
和v
值是否在中的限制范围内
for(i=0;i<G->E;i++)
{
printf("Enter the source and destination : ");
scanf("%d %d", &u,&v);
if(u<G->V && v<G->V)
{
G->adj[u][v]=1;
G->adj[v][u]=1;
}
else
{
printf("\ninvalid value. Try again.");
i--;
}
}
请注意,如果分配内存失败,malloc()
将返回NULL
。您需要检查malloc()
调用是否也失败
而且,您不必在C.Read中转换
malloc()
的返回值。正如您提到的,错误在那里
G->adj = malloc(sizeof(G->V * G->E));
//Initialization of vertices
for(i=0;i<=G->V;i++)
{
for(j=0;j<=G->V;j++)
{
G->adj[i][j]=0;
}
}
G->adj=malloc(sizeof(G->V*G->E));
//顶点初始化
对于(i=0;iV;i++)
{
对于(j=0;jV;j++)
{
G->adj[i][j]=0;
}
}
- 你在
写作,在那里你分配了adj[V][V]
的大小,这将是sizeof(G->V*G->E)
(一个int),即使你想要达到sizeof(int)
adj[V][E]
- 此外,在分配1D数组和访问2D数组时,访问
将首先尝试读取adj[0][0]
作为指向数组的指针(未定义的值),然后尝试写入adj[0]
undefined[0]
malloc(G->V*G->V*sizeof(int))进行分配
并使用adj[i*V+j]进行访问/写入
您的逻辑中有相当多的错误,这是因为您期望代码的行为与它真正理解您的方式不同。使用调试器了解故障发生的位置并检查涉及的变量可能很有用
编辑:
还提到:
您正在为G
分配一个小的大小,因为malloc(sizeof(struct-Graph*)
相当于malloc(sizeof(void*))
(分配一个指针的大小),在这里您应该malloc(sizeof(struct-Graph))
第二次编辑:
注意到您的
j
循环for(j=0;iV;j++
应该是for(j=0;jV;j++)
,正如您所提到的,您的错误就在那里
G->adj = malloc(sizeof(G->V * G->E));
//Initialization of vertices
for(i=0;i<=G->V;i++)
{
for(j=0;j<=G->V;j++)
{
G->adj[i][j]=0;
}
}
G->adj=malloc(sizeof(G->V*G->E));
//顶点初始化
对于(i=0;iV;i++)
{
对于(j=0;jV;j++)
{
G->adj[i][j]=0;
}
}
- 你在
写作,在那里你分配了adj[V][V]
的大小,这将是sizeof(G->V*G->E)
(一个int),即使你想要达到sizeof(int)
adj[V][E]
- 此外,在分配1D数组和访问2D数组时,访问
将首先尝试读取adj[0][0]
作为指向数组的指针(未定义的值),然后尝试写入adj[0]
undefined[0]
malloc(G->V*G->V*sizeof(int))进行分配
并使用adj[i*V+j]进行访问/写入
您的逻辑中有相当多的错误,这是因为您期望代码的行为与它真正理解您的方式不同。使用调试器了解故障发生的位置并检查涉及的变量可能很有用
编辑:
还提到:
您正在为G
分配一个小的大小,因为malloc(sizeof(struct-Graph*)
相当于malloc(sizeof(void*))
(分配一个指针的大小),在这里您应该malloc(sizeof(struct-Graph))
第二次编辑:
注意到你的
j
循环for(j=0;iV;j++)
中有一个拼写错误,应该是for(j=0;jV;j++)
我做了。。。但它仍然显示出分割错误。。。。顶点的/ /初始化问题
G->adj = malloc(sizeof(G->V * G->E));
//Initialization of vertices
for(i=0;i<=G->V;i++)
{
for(j=0;j<=G->V;j++)
{
G->adj[i][j]=0;
}
}