2011-12-17 69 views
0

也许有人可以帮助我。在我的项目中,我正在使用具有动态分配的链接列表。我不知道为什么,但它只是不工作:(为什么我在这里得到一个SIGABRT(动态分配)?

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

这里有一个问题:

newWord->word = malloc(wcslen(string) * sizeof(wchar_t)); 
wcscpy(newWord->word, string); 

您忘记为终止空字符分配空间。

newWord->word = malloc((wcslen(string) + 1) * sizeof(wchar_t)); 
相关问题