简体   繁体   English

为什么在这里获得SIGABRT(动态分配)?

[英]Why do I get a SIGABRT here(dynamic allocation)?

Maybe somebody can help me. 也许有人可以帮助我。 In my project I'm using a linked list with dynamic allocation. 在我的项目中,我使用的是动态分配的链表。 And I don't know why, but it just doesn't work :( 而且我不知道为什么,但是它不起作用:(

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;
}

When I'm trying to compile that, I'm just seeing 当我尝试编译时,我只是看到

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.

Aborted 中止

Maybe can somebody tell why I have this troubles? 也许有人能说出我为什么有这个麻烦? Thanks :) 谢谢 :)

Here's one problem: 这是一个问题:

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

You forgot to allocate space for the terminating null character. 您忘记为终止空字符分配空间。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM