I haven't found a similar question for the problem I've ran into. I am only guessing the root of the error, but I don't have enough knowledge to debug it. The code is as follows:
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;
}
Everything works (I've tested printing the matrix inside the function, before returning the graph struct), but it stops after returning the adjacency_matrix struct called by my function in the main source file.
Visual Studio says
Unhandled exception at 0x77F4A879 (ntdll.dll) in dijkstra.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77F85910).
then points me to exe_common.inl, on line 292.
if (!has_cctor)
_cexit();
I would appreciate some help.
I think you need to allocate more memory for the columns of the matrix:
for (i = 0; i <= graph.vertices; i++)
graph.matrix[i] = (int*)malloc((graph.vertices + 1) * sizeof(int));
User contributions licensed under CC BY-SA 3.0