我已經編寫了一個代碼來將新成員添加到列表中。當我增加兩名成員時,它的工作正常。但是,只要我添加第三個並編譯並運行代碼給我一個錯誤。代碼和錯誤如下:./a.out是後鏈接列表插入導致sysmalloc斷言錯誤
#include<stdio.h>
#include<stdlib.h>
struct list_no {
struct list_no *prev,*nest;
struct linked_list *fnl;
int seq_no;
};
static int counter = 0;
struct list_no *lists,*first;
int add_list()
{
struct list_no *temp;
counter++;
if(lists == '\0')
{
lists = (struct list_no *)malloc(sizeof(struct list_no));
lists->prev = '\0';
lists->nest = '\0';
lists->fnl = '\0';
lists->seq_no = 1;
first = lists;
}
else
{
temp = lists;
lists->nest = (struct list_no *)malloc(sizeof(struct linked_list));
lists = lists->nest;
lists->fnl = '\0';
lists->prev = temp;
lists->nest = NULL;
lists->seq_no = counter;
}
return 0;
}
int main()
{
int i=0,lcount;
int i=0,lcount;
char ch = 'y';
first = '\0';
lists = first;
int w;
while(i != 3)
{
add_list();
printf("\n the val ::%d\n",lists->seq_no);
i++;
}
return 0;
}
錯誤消息來了:
the val ::1
the val ::2
a.out: malloc.c:3097: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted
爲什麼它不能添加第三個成員?
這是一個調試器的用途。 – 2012-01-30 08:47:56
怎麼樣一個適當的主題?! – ezdazuzena 2012-01-30 08:50:05
@OliCharlesworth嗨它的代碼看起來很好....你能給我什麼建議什麼可能是錯的 – karan421 2012-01-30 08:50:36