gpt4 book ai didi

c - 二维数组的动态分配

转载 作者:太空宇宙 更新时间:2023-11-04 02:55:48 25 4
gpt4 key购买 nike

我正在使用邻接矩阵实现图形,但我无法解决分割错误。谁能帮我指导二维矩阵的动态分配?我还想知道二维数组如何存储在内存中以及如何访问它。

#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;
}

最佳答案

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;
}
}

下面我不确定的部分,你手动将边缘设置为一个,对吧?你不应该使用 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。每次遇到段错误时,这都会创建一个核心转储文件。

然后查看问题出在哪里,运行 gdb your_app core 并在内部运行 backtrace。在这些情况下,我无法强调使用调试器的重要性。

关于c - 二维数组的动态分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com