在代码中,您在Graph中分配了一个由4个Node指针组成的数组,称为head。
// Data structure to store graph struct Graph { // An array of pointers to Node to represent adjacency list struct Node* head[N]; };
稍后,您将通过初始化内存来触摸不是您的内存:
// initialize head pointer for all vertices for (i = 0; ihead[i] = NULL;
使用valgrind:
=16498== Invalid write of size 8 ==16498== at 0x400653: createGraph (a.c:37) ==16498== by 0x400815: main (a.c:103) ==16498== Address 0x52044a0 is 0 bytes after a block of size 32 alloc'd ==16498== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==16498== by 0x40063E: createGraph (a.c:33) ==16498== by 0x400815: main (a.c:103)