简体   繁体   English

二维数组动态分配中的分割错误

[英]Segmentation fault in 2D array Dynamic Allocation

#include<stdio.h>
#include<stdlib.h>
struct Graph{
int V;
int E;
int **Adj;
};
struct Graph* adjMatrix(){
int u,v,i;
struct Graph *G;
G=(struct Graph*)malloc(sizeof(struct Graph));
if(!G){
    printf("Memory Error!\n");
    return;
}
printf("Enter number of nodes and number of edges:\n");
scanf("%d %d",&G->V,&G->E);
G->Adj=malloc((G->V)*(G->V)*sizeof(int));
for(u=0;u<(G->V);u++)
    for(v=0;v<(G->V);v++)
        G->Adj[u][v]=0; //This gives a segmentation fault.


printf("Enter node numbers in pair that connect an edge:\n");
for(i=0;i<(G->E);i++){
    scanf("%d %d",&u,&v);
    G->Adj[u][v]=1;
    G->Adj[v][u]=1;
}
return(G);
}
int main(){
struct Graph *G;
int i,j,count=0;
G=adjMatrix();
for(i=0;i<G->V;i++){
    for(j=0;j<G->V;j++){
        printf("%d ",G->Adj[i][j]);
        count++;
    }
    if(count==G->V)
        printf("\n");
}
return 0;
}

The code shows segmentation fault when I tried to assign a value in 2D array ie at G->Adj[u][v]=0;当我尝试在 2D 数组中分配一个值时,代码显示分段错误,即 G->Adj[u][v]=0; but I don't know what is wrong with that?但我不知道这有什么问题? Because It's simply a assignment to the array.因为它只是对数组的赋值。

The segmentation fault is casued due-to error at the 2D array allocation method, as it us done at the following line:分段错误是由于二维数组分配方法中的错误引起的,正如我们在以下行中所做的那样:

G->Adj=malloc((G->V)*(G->V)*sizeof(int)); . .

which actually allocated a 1D buffer of (G->V)*(G->V) integers, so it can't enable a later access in syntax of 2D, like you want它实际上分配了一个 (G->V)*(G->V) 整数的一维缓冲区,所以它不能像你想要的那样启用二维语法的后续访问

In a nutshell: when allocation 2D array like you wish, you should first allocate the 1D array of pointers.简而言之:当你想分配二维数组时,你应该首先分配一维指针数组。 At your code, should be:在您的代码中,应该是:

G->Adj = (int **)malloc(sizeof(int *)*G->V);

And then allocate G->V vectors per pointer:然后为每个指针分配 G->V 向量:

for(i=0; i < G->V; i++) 
{
    G->Adj[i] = (int *)malloc(sizeof(int)*G->V);
}

furthermore, a good practice is to verify allocation result is not NULL (malloc failure), for each allocation此外,一个好的做法是验证分配结果不是 NULL(malloc 失败),对于每个分配

for general explanation on c vectors allocation you may read more at Method 2: the "can still use [r][c] syntax to access" way有关 c 向量分配的一般说明,您可以在 方法 2 中阅读更多内容 :“仍然可以使用 [r][c] 语法访问”方式

Besides that, memory release is missing at the end of your program, so you should add calls to free() at the opposite order (vectors and then pointers)除此之外,程序结束时缺少内存释放,因此您应该以相反的顺序添加对 free() 的调用(向量然后是指针)

#include<stdio.h>
#include<stdlib.h>
struct Graph{
int V;
int E;
int **Adj;
};
struct Graph* adjMatrix(){
int u,v,i;
struct Graph *G;
G=malloc(sizeof(struct Graph));
if(!G){
    printf("Memory Error!\n");
    return 0;
}
printf("Enter number of nodes and number of edges:\n");
scanf("%d %d",&G->V,&G->E);
//First problem was here this is how you allocate a 2D array dynamically 
G->Adj=malloc((G->V)*sizeof(int*));
for(u=0;u<G->V;u++)
    G->Adj[u]=malloc((G->V)*sizeof(int));
for(u=0;u<(G->V);u++)
    for(v=0;v<(G->V);v++)
        G->Adj[u][v]=0; //This gives a segmentation fault.

    // i added some adjustment here to help you 
       printf("Enter node numbers in pair that connect an edge:\n");
    for(i=0;i<(G->E);i++){
    scanf("%d %d",&u,&v);
    if(u>=G->V || v>=G->V){
    printf("Error give the right input\n");
    i--;}
    else{
    G->Adj[u][v]=1;
    G->Adj[v][u]=1;}
}
return(G);
}
int main(){
struct Graph *G;
int i,j,count=0;
G=adjMatrix();
for(i=0;i<G->V;i++){
    for(j=0;j<G->V;j++){
        printf("%d ",G->Adj[i][j]);
        count++;
    }
    if(count==G->V)
        printf("\n");
}
return 0;
}

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

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