1
Windows has triggered a breakpoint in Graph.exe.

This may be due to a corruption of the heap, which indicates a bug in Graph.exe     or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Graph.exe has focus.

The output window may have more diagnostic information

我的代码中没有任何断点,也没有按 F12。这是我的代码。怎么了?printf("sizeof edge : %d\n",sizeof(edge));这一行会出错。我不明白为什么出了什么问题?

#include <stdio.h>
#include <stdlib.h>



typedef struct HeapStruct heap;
typedef struct edge edge;
struct edge
{
    int start,end,weight;
};

struct HeapStruct {
    int Capacity;
    int Size;
    edge *head;
};


void init(int * sets,int size);
int unionsets(int * sets, int i, int j);
int find(int * sets, int i);
void buildHeap(heap h);
edge deleteMin(heap * h);
int ends(int * sets,int size);
int main()
{
    int V,E,*sets,a,startv,endv,weight;
    char c,h;
    edge ed;
    edge * ee;
    heap * Heap;
    Heap = (heap*)malloc(sizeof(heap));
    printf("sizeof edge : %d\n",sizeof(edge));//this line
    scanf("%d",&V);
    sets = (int*)malloc(sizeof(int)*V);
    init(sets,V);
    scanf("%d",&E);
    Heap->head =  (edge*)malloc(sizeof(edge)*E);//and this line
    Heap->Capacity = E;
    Heap->Size=0;

    for(a=0; a<E; a++)
    {
        scanf("%d%c%d%c%d",&startv,&c,&endv,&h,&weight);
        Heap->head[Heap->Size].end = endv;
        Heap->head[Heap->Size++].start = startv;
        Heap->head[Heap->Size++].weight = weight;
    }
    buildHeap(*Heap);
    do
    {
        ed = deleteMin(Heap);
        if(find(sets,ed.start)<0 || find(sets,ed.end)<0 || find(sets,ed.start) != find(sets,ed.end))
        {
            unionsets(sets,ed.start,ed.end);
            printf("%d,%d,%d\n",ed.start,ed.end,ed.weight);
        }
    }
    while(ends(sets,V));

    scanf("%d%c%d%c%d",&startv,&c,&endv,&h,&weight);
    return 0;

    }
4

2 回答 2

2

Windows 已在 Graph.exe 中触发断点。

字面意思是,操作系统本身使调试器停止。当您在任何最新的 Windows 版本上调试程序时,您将获得 Windows 内存管理器的调试版本。这增加了额外的检查,以确保您的程序不会破坏堆。当它检测到堆损坏时,它会中断程序告诉你它。

很有用。接下来您需要做的是仔细检查您的代码,以确保它没有写入未分配的内存。然后,您将获得以下声明:

    Heap->head[Heap->Size++].start = startv;

与该代码中的其他语句一起假定该数组包含 3 * E 元素,但您只分配了 E 元素。

轰隆隆!

于 2013-06-01T15:47:38.220 回答
0
printf("sizeof edge : %d\n",sizeof(edge));

这可能会使 64 位系统崩溃,因为sizeof返回一个 64 位数字但%d只需要 32 位。试试%zd吧。

是的,C 很挑剔。不过,您的编译器应该对此给出警告。

于 2013-06-01T14:45:59.213 回答