二维阵列的动态分配

时间:2013-07-11 01:56:38

标签: c graph segmentation-fault multidimensional-array

我使用邻接矩阵实现图形,但我无法解决分段错误。任何人都可以帮助我指导二维矩阵的动态分配吗?我还想知道2-D数组如何存储在内存中以及如何访问它。

#include<stdio.h>
#include<stdlib.h>

struct Graph{
int V; // To represent number the vertex...
int E; //To represent number the Edge.....
int **Adj; // Two dimensional matrix to form the adjacency matrix... 
};


struct Graph *adjMatrixOfGraph(){

        int i;    //for scanning the edges between them .... 
        int u,v; // for loop while initliasing the  adjacency matrix... 
        struct Graph *G=(struct Graph*) malloc(sizeof(struct Graph)); //

        if(!G){
        printf("Memory Error");
        return;
        }

        printf("Number of Vertices");

        scanf("%d",&G->V);
        printf("%d",G->V);
        printf("Number of Edges");
        scanf("%d",&G->E);
        G->Adj=(int **)malloc(sizeof(G->V * G->V)); //allocating memory for G->Adj);
        /*Dynamic memory allocation for Two Dimensional Arrays */

/*      G->Adj = malloc(G->V * sizeof(int )); 
            if(G->Adj == NULL) {         
                 printf( "out of memory\n");     
                }     

         for(i = 0; i < G->V; i++) {     
                G->Adj[i] = malloc(G->V * sizeof(int ));     
                if(G->Adj[i] == NULL) {         
                printf( "out of memory\n");     


                } 
                }
*/

        if(!G->Adj)
        {
        printf("Memory Error");
        return;
        }


        for(u=0;  u < G->V; u++){
        for(v=0; v < G->V; v++){
         //printf("%d %d",u,v); 
         G->Adj[u][v]=0;  //initalising the complete adjacency matrix to zero.
        }
        }

        //Enter the edges.. and the vertices.
        //We are considering this graph as undirected one ... 
        for(i=0;i< G->E;i++)
        {
        scanf("Reading Edges %d %d ",&u,&v);
        G->Adj[u][v]=1;
        G->Adj[u][v]=1;

        //if this graph was directed then we should have considere only one side... 
        //G->V[u][v]=1;

        }


return G;
}

main()
{
struct Graph *G1=adjMatrixOfGraph();

//struct Graph *adjMatrixOfGraph(){
printf("Successful");
return 0;
}

2 个答案:

答案 0 :(得分:1)

int **Adj分配内存的方式如下:

首先,为指向整数的指针数分配内存:

Adj = malloc(sizeof(int*) * number_of_integers); /* notice what I pass to sizeof */

接下来,为每个整数分别分配内存:

for (i = 0; i < number_of_integers; i++)
    Adj[i] = malloc(sizeof(int) * G->E);

当然,每个malloc来电都需要以类似的方式进行free来电。

请注意,我没有cast the result of malloc

我对您的代码进行了一些其他更改:

更新scanf以确保缓冲区中剩余的新行没有问题:

    printf("Number of Vertices: ");
    scanf(" %d", &G->V);
    printf("Number of Edges: ");
    scanf(" %d", &G->E);

初始化它们(或者,查找calloc,因为它为你做零初始化):

    for(u=0;  u < G->V; u++) // for each vertice
    {
        for(v=0; v < G->E; v++) // for each edge
        {
            G->Adj[u][v] = 0;
        }
    }

下面的部分我不确定,你手动将边缘设置为1,对吗?您不应该使用G->V而不是G->E吗?

    for(i = 0; i < G->V; i++)
    {
        printf("Reading vertice u: ");
        scanf(" %d",&u);
        printf("Reading edge v: ");
        scanf(" %d",&v);

        if (u > G->V || v > G->E) // simple error handling 
        {
            printf("Input bigger than size of vertice/edges\n");
            exit(1);
        }

        G->Adj[u][v] = 1;

        G->Adj[u][v] = 1;
    }

之后我能够打印Successful。如果您想更轻松一点,请使用-g标志编译代码,如果您使用Linux,请执行ulimit -c unlimited。每次出现段错误时,都会创建一个coredump文件。

然后查看问题所在,运行gdb your_app core并运行backtrace。我不能强调在这些情况下使用调试器的重要性。

答案 1 :(得分:1)

int **allocate_2D_array(int rows, int columns)
{
    int k = 0;
    int **array = malloc(rows * sizeof (int *) );

    array[0] = malloc(columns * rows * sizeof (int) );
    for (k=1; k < rows; k++)
    {
        array[k] = array[0] + columns*k;
        bzero(array[k], columns * sizeof (int) );
    }

    bzero(array[0], columns * sizeof (int) );

    return array;
}