I am working on a program on C++ that's dealing with graphs.
I store graph as an adjacency list of nodes, and I have the corresponding structures declared in .h file as follows:
typedef struct Node {
int val;
struct Node * next;
} node;
typedef struct Graph {
int v;
node ** arr;
node ** arr2; // reserved list for a reversed directed graph.
} graph;
I have a function for initializing a graph defined as follows:
graph * creategraph(int v) { // v == number of vertices
int i;
graph * temp = (graph*)malloc(sizeof(graph));
temp->v = v;
for(i = 0; i < v; i++) {
temp->arr = (node**)malloc(v*sizeof(node*));
}
for(i = 0; i < v; i++) {
temp->arr[i] = NULL;
}
return temp;
}
I call the function as shown below to create a graph with number of vertices being equal to num_vertices
:
graph * g = creategraph (num_vertices);
With num_vertices
being equal to 200000
, the "Access Violation Writing Location" exception is raised in graph * createGraph
on the first execution of temp->arr[i] = NULL;
.
Could anyone tell me what's the problem here? Thank you.