Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我正在使用邻接列表实现一个图_C - Fatal编程技术网

C 我正在使用邻接列表实现一个图

C 我正在使用邻接列表实现一个图,c,C,我得到以下错误 a、 out:malloc.c:2369:sysmalloc:Assertion`old_top==mbinptr char*&av->bin[1-1*2]-__ 结构malloc_块的内置偏移量,fd&&old_size==0 | |无符号长旧_size>=无符号长 __结构malloc块的内置偏移量,fd_nextsize+2*sizeofsize\u t-1和~2*sizeofsize\u t -1&&old\u top->size&0x1&&unsigned longol

我得到以下错误

a、 out:malloc.c:2369:sysmalloc:Assertion`old_top==mbinptr char*&av->bin[1-1*2]-__ 结构malloc_块的内置偏移量,fd&&old_size==0 | |无符号长旧_size>=无符号长 __结构malloc块的内置偏移量,fd_nextsize+2*sizeofsize\u t-1和~2*sizeofsize\u t -1&&old\u top->size&0x1&&unsigned longold\u end&pagemask==0'失败。
已中止的堆芯转储

您确定这些scanf调用正常吗?您确定输入值都在0范围内吗。。V-1

我建议为这两种类型都添加错误检查


此外,第一个ptr和ptr1 Malloc未使用。

第一件引人注目的事情:

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

struct node
{
    int value;
    struct node* next;
} *graph;

int main(void)
{
    int V,E,i,u,v;
    struct node *ptr,*ptr1;
    ptr=malloc(sizeof(struct node *));
    ptr1=malloc(sizeof(struct node *));
    scanf("%d",&V);
    graph=malloc(V*(sizeof(struct node *)));
    if(!graph)
        printf("Not allocated");

    for(i=0;i<V;i++)
    {
        graph[i].next=NULL;
    }

    scanf("%d",&E);
    for(i=0;i<E;i++)
    {
        //printf("**\n");
        scanf("%d %d",&u,&v);
        ptr=malloc(sizeof(struct node *));
        ptr1=malloc(sizeof(struct node *));
        ptr->value=u;
        ptr->next=graph[v].next;
        graph[v].next=ptr;
        ptr1->value=v;
        ptr1->next=graph[u].next;
        graph[u].next=ptr1;
    }

    for(i=0;i<V;i++)
    {

        ptr=graph[i].next;
        printf("**\n");
        printf("%d ===>\n",i);
        while(ptr)
        {
            printf("%d->",ptr->value);
            ptr=ptr->next;
        }
        printf("NULL\n");
    }
}

ptr和ptr1旨在指向结构节点,但您可以为结构节点*分配空间。对于图表,同样的评论。

谢谢。我错了。但在一些程序中,mallocsizeofstruct node*起作用了,对此有什么解释吗?未定义的行为。C和C++不检查内存溢出,所以在太小的分配块中写入太大的对象只是覆盖了它的内容。如果幸运的话,程序会崩溃,你会看到你的错误。如果您没有注意到,这个bug就会被忽略,直到它在以后的另一次更改后随机崩溃。
struct node *ptr,*ptr1;
ptr=malloc(sizeof(struct node *));
ptr1=malloc(sizeof(struct node *));