入力ファイルの形式(u、v、weight)のグラフデータの大規模なセットに対して、幅優先探索とベルマンフォードアルゴリズムを実行しています。
幅優先探索で初期化します。すべての頂点は、未訪問の場合は0とマークする必要があります。
プログラムの後半では、プログラムの最後ではなく、エッジを追加するたびにBFSを呼び出すためです(これは、ベルマンフォード法とBFSに関する研究プロジェクトの一部ですが、まったく意味がありません)。頂点配列を未訪問に再初期化します。
ただし、より大きなセットを実行すると、頂点配列を再初期化したときにセグメンテーション違反が発生するようです。私は、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)
キュー機能を確認してください:
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条件の行です