我有問題,我的學校項目,該項目正在測試這個版本的服務器返回此錯誤:分割故障C,malloc的陣列
./__runscript: line 2: 489 Segmentation fault ./a.out < __input
編譯是確定的,但它開始運行就當顯示此錯誤。但如果我在Windows 7上使用visual studio 10運行它,一切都很好。我認爲這個錯誤可能在insert()函數中。
POSTUP *insert(POSTUP *first)
{
POSTUP* current,*pom;
current=(POSTUP*)malloc(sizeof(POSTUP));
pom=(POSTUP*)malloc(sizeof(POSTUP));
int i,count_of_elements;
scanf("%d", &count_of_elements);
if(first==NULL)
{
first=current;
first->array= (int*)malloc(count_of_elements*sizeof(int));
for(i=0; i<count_of_elements; i++)
{
scanf("%d",&first->array[i]);
}
first->elements=count_of_elements;
first->next=NULL;
return first;
}
else
{
pom->array= (int*)malloc(count_of_elements*sizeof(int));
for(i=0; i<count_of_elements; i++)
{
scanf("%d",&pom->array[i]);
}
pom->next=NULL;
pom->elements=count_of_elements;
current=first;
while(1)
{
if(current->next==NULL)
{
current->next=pom;
return first;
}
}
}
return 0;
}
int main(void)
{
int count, i;
POSTUP* first,*current;
first= (POSTUP*)malloc(sizeof(POSTUP));
first=NULL;
scanf("%d",&count);
for(i=0; i<count; i++)
{
first=insert(first);
}
}
'pom'如果'first'是'NULL'就會泄漏,否則'current'會泄漏。爲什麼不分配一個新節點? – nneonneo
'scanf(「%d」,&prvy-> array [i]);' - 爲什麼不'&first-> array [i]'在這裏?什麼是「prvy」? – nneonneo
每次都會發生主要泄漏.... –