也许有人可以帮助我。在我的项目中,我正在使用带有动态分配的链表。我不知道为什么,但它不起作用:(
void insertLast (TList *list, wchar_t *string) {
TWord *newWord;
if ((newWord = malloc (sizeof(TWord))) == NULL)
exit (EXIT_FAILURE);
newWord->prev = list->tail;
newWord->next = NULL;
newWord->word = malloc(wcslen(string) * sizeof(wchar_t));
wcscpy(newWord->word, string);
if (list->tail != NULL) {
list->tail->next = newWord;
} else {
list->head = newWord;
}
list->tail = newWord;
}
当我试图编译时,我只是看到了
lab: malloc.c:3096: 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.
中止
也许有人可以告诉我为什么会遇到这种麻烦?谢谢:))
答案 0 :(得分:0)
这是一个问题:
newWord->word = malloc(wcslen(string) * sizeof(wchar_t));
wcscpy(newWord->word, string);
您忘记为终止空字符分配空间。
newWord->word = malloc((wcslen(string) + 1) * sizeof(wchar_t));