#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");
}
}
實現的圖形,我收到以下錯誤我使用鄰接表
的a.out:malloc.c:2369:SYSMALLOC:斷言`(old_top ==(((mbinptr)((( char *)&((av) - > bins [((1) - 1)* 2]))__ builtin_offsetof(struct malloc_chunk,fd))))& & old_size == 0)|| || ((unsigned long)(old_size)> =(unsigned long))&〜((2 *(無符號長整數)的sizeof(爲size_t) )) - 1)))& &((old_top) - >大小爲0x1 &)& &((無符號長整數)OLD_END & pagemask)== 0)」失敗。 Aborted(核心轉儲)
謝謝。得到了我的錯誤。 但是在某些程序中「malloc(sizeof(struct node *))有效,是否有解釋? – user2711221 2014-09-29 03:22:46
*未定義的行爲* .C和C++不檢查內存溢出,所以也寫太大的對象如果你幸運的話,程序崩潰,你會看到你的錯誤,如果你沒有,錯誤就會被忽視,直到它在隨後的另一個改變方式中隨機崩潰。 – Quentin 2014-09-29 11:09:19