2013-06-01 72 views
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));該行發生該錯誤。 我不明白爲什麼 有什麼問題?sizeof(structname)是這個錯誤嗎?

#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; 

    } 

回答

0
printf("sizeof edge : %d\n",sizeof(edge)); 

這可能會崩潰一個64位系統,因爲sizeof返回一個64位的數,但%d預計只有32位。改爲嘗試%zd

是的,C是挑剔的。不過,你的編譯器應該給出一個警告。

+0

我修復了它,並試圖再次調試。但是,這仍然是一個錯誤。 – user2443387

+0

@ user2443387相同的錯誤或不同?你是如何跟蹤這些錯誤的? – Potatoswatter

+0

相同的錯誤..... – user2443387

2

Windows在Graph.exe中觸發了一個斷點。

從字面上看,操作系統本身使調試器停止。當您在任何最新的Windows版本上調試程序時,您將獲得Windows內存管理器的調試版本。這增加了額外的檢查,以確保您的程序不會破壞堆。當它檢測到堆損壞時,它會中斷程序來告訴你。

非常有用。接下來你需要做的是仔細檢查你的代碼,以確保它沒有寫入未分配的內存。然後你會降落在此聲明:

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

與在代碼中的其他語句一起假定該數組包含3個* E元素,但你只分配元件E。

Kaboom!