重新初始化arrays是否会产生段错误?

我正在对输入文件格式(u,v,weight)的大量图形数据运行宽度优先搜索和贝尔曼福特算法。

我在广度优先搜索中初始化,所有顶点都应标记为0以供未访问。

后来在程序中,因为我每次添加边缘后都会调用BFS,而不是在程序结束时(这是关于Bellman ford和BFS的研究项目的一部分,即使它没有多大意义)我将顶点数组重新初始化为unvisited。

但是,当我重新初始化顶点数组时运行更大的集合时,我遇到了分段错误。 我假设有更大的集合,因为我有一些较小的测试数据集,从8个顶点到10,然后在100和更大,它失败了。

以下是我在程序开始时初始化的方法:

for(i=0;i<numberVertices;i++) { vertexArray[i].v = i+1; vertexArray[i].adj = NULL; vertexArray[i].marked = 0; if(i==0) { vertexArray[i].weight = 0; } else{ vertexArray[i].weight = 10000; } } 

以下是我在BFS结束后直接到达文件末尾时,在while循环结束时重新初始化的方式:

 BFS(q, u) for(i=0;i<numberVertices;i++) { vertexArray[i].marked = 0; } 

就像我说它似乎适用于较小的数据集,但我不明白为什么当我重新初始化它似乎是错误的。

请让我知道你的想法和建议!

Valgrind输出示例案例:

 ==6634== Memcheck, a memory error detector ==6634== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==6634== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info ==6634== Command: ./a.out 100S.txt ==6634== ==6634== Conditional jump or move depends on uninitialised value(s) ==6634== at 0x400C13: checkonqueue (bfs.c:160) ==6634== by 0x400B7F: BFS (bfs.c:142) ==6634== by 0x40094A: main (bfs.c:103) ==6634== ==6634== Invalid write of size 4 ==6634== at 0x40096E: main (bfs.c:107) ==6634== Address 0x4d00000041 is not stack'd, malloc'd or (recently) free'd ==6634== ==6634== ==6634== Process terminating with default action of signal 11 (SIGSEGV) ==6634== Access not within mapped region at address 0x4D00000041 ==6634== at 0x40096E: main (bfs.c:107) ==6634== If you believe this happened as a result of a stack ==6634== overflow in your program's main thread (unlikely but ==6634== possible), you can try to increase the size of the ==6634== main thread stack using the --main-stacksize= flag. ==6634== The main thread stack size used in this run was 10485760. ==6634== ==6634== HEAP SUMMARY: ==6634== in use at exit: 3,448 bytes in 181 blocks ==6634== total heap usage: 181 allocs, 0 frees, 3,448 bytes allocated ==6634== ==6634== LEAK SUMMARY: ==6634== definitely lost: 0 bytes in 0 blocks ==6634== indirectly lost: 0 bytes in 0 blocks ==6634== possibly lost: 0 bytes in 0 blocks ==6634== still reachable: 3,448 bytes in 181 blocks ==6634== suppressed: 0 bytes in 0 blocks ==6634== Reachable blocks (those to which a pointer was found) are not shown. ==6634== To see them, rerun with: --leak-check=full --show-reachable=yes ==6634== ==6634== For counts of detected and suppressed errors, rerun with: -v ==6634== Use --track-origins=yes to see where uninitialised values come from ==6634== ERROR SUMMARY: 16120 errors from 2 contexts (suppressed: 6 from 6) Segmentation fault (core dumped) 

检查队列function:

 int checkonqueue(int * q, int value) { int i = 0; for(i=0;i < max;i++) { if(q[i] == value) { return 1; } } return 0; } 

第160行是if条件的行