0

可能重复:
重新初始化阵列正在创建段错误?

我在具有给定权重的边输入上运行 bellman ford 和 BFS(顶点、顶点b、权重)。每次阅读最新的边缘后,我都会运行 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 后,我必须重新初始化标记为 0,以便 BFS 再次工作,因此我重新初始化数组:

numberUseful += BFS(vertexArray, q, u);

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

并且该行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)

我被告知在与我的段错误(107)相同的行和作为 checkonqueue 函数的一部分的第 160 行给了我错误:

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

哪里if(q[i] == value)出现了内存问题,所以我被告知。

我认为这可能是内存问题的原因是它适用于较小的集合(或者看起来如此!)

如果您知道如何真正使用 valgrind 或发现错误,我将不胜感激!

BFS:

while(front != -1)
{
    u = dequeue(q);
    if(vertexArray[u-1].adj != NULL)
    {
        adjacent = vertexArray[u-1].adj;
        vertexArray[u-1].marked = 1;


        while(adjacent != NULL)
        {
            v = adjacent->v;
            if(distance[vertexArray[v-1].v-1] > distance[vertexArray[u-1].v-1] + adjacent->edgeWeight)
            {
                distance[vertexArray[v-1].v-1] = distance[vertexArray[u-1].v-1] +adjacent->edgeWeight;
                success = 1;

            }

            int check = 0;
            check = checkonqueue(q, v);
            if(check == 0 && vertexArray[v-1].marked == 0)
            {
                enqueue(q, v);
            }
            adjacent = adjacent->next;
        }
    }
}
return success;
4

0 回答 0