Question

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 :)

Was it helpful?

Solution

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top