sizeof(structname)这个错了吗?

时间:2013-06-01 14:38:06

标签: c dll heap

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;

    }

2 个答案:

答案 0 :(得分:2)

  

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

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

非常有用。您接下来需要做的是仔细检查您的代码,以确保它没有写入未分配的内存。然后你将发表这个声明:

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

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

KABOOM!

答案 1 :(得分:0)

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

这会导致64位系统崩溃,因为sizeof返回64位数字,但%d只需要32位。请改为%zd

是的,C很挑剔。但是,您的编译器应该对此发出警告。

相关问题