我重新初始化数组时可能是内存问题?

时间:2012-11-04 00:53:44

标签: c arrays memory-management initialization valgrind

  

可能重复:
  Reinitializing an Array is creating a segfault?

我正在使用格式(vertexa,vertexb,weight)中具有给定权重的边输入运行bellman ford和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后我必须重新初始化标记为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)和第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;

0 个答案:

没有答案