对于我遇到过的问题,我没有找到类似的问题。我只是猜测错误的根源,但我没有足够的知识来调试它。代码如下:
的main.c
#include <stdio.h>
#include "adjacency.h"
int main()
{
FILE* textfile;
textfile = fopen("graf.txt", "r");
adjacency_matrix Graph;
create_graph(textfile);
}
adjacency.c
#include "adjacency.h"
#include <stdlib.h>
#include <stdio.h>
adjacency_matrix create_graph(FILE* input)
{
adjacency_matrix graph; int s_node, f_node;
fscanf(input, "%d", &graph.vertices);
int i, j;
graph.matrix = (int**)malloc((graph.vertices + 1) * sizeof(int*));
for (i = 0; i <= graph.vertices; i++)
graph.matrix[i] = (int*)malloc(sizeof(int));
for (i = 0; i <= graph.vertices; i++)
for (j = 0; j <= graph.vertices; j++)
graph.matrix[i][j] = 0;
for (i = 0; i <= graph.vertices; i++)
graph.matrix[i][0] = graph.matrix[0][i] = i;
while (1)
{
fscanf(input, "%d %d", &s_node, &f_node);
if (feof(input))
break;
graph.matrix[s_node][f_node] = 1;
}
return graph;
}
一切正常(我已经测试了在返回图形结构之前在函数内打印矩阵),但在主函数文件中返回my function调用的adjacency_matrix结构后停止了。
Visual Studio说
Unhandled exception at 0x77F4A879 (ntdll.dll) in dijkstra.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77F85910).
然后将我指向exe_common.inl,在第292行。
if (!has_cctor)
_cexit();
我将不胜感激。
答案 0 :(得分:0)
我认为您需要为矩阵的列分配更多内存:
for (i = 0; i <= graph.vertices; i++)
graph.matrix[i] = (int*)malloc((graph.vertices + 1) * sizeof(int));