我在运行tries程序时总是出错。
a.out:malloc.c:2372:sysmalloc:Assertion`(old_-top==((mbinptr)(char*)&(av)>>bins[((1)-1)*2]))-\u-builtin_offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offsetof offset long long==(old_size)>=(unsignlong long long long)(unsigned long long)(unsigned long)(unsigned long)(offsetof offsetof offsetof offsetof offset((2*(sizeof(size-t)))-1)和~(2*(sizeof(size-t))-1)&&((old_top)—>大小&0x1)&&((unsigned long)old_end&pagemask)==0)失败。
我的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct nodeData
{
    char ch;                /* This structure looks like a linked list */
    struct nodeData *next;
}node;

typedef struct tries
{
    node *start[26];
}tries;

tries *makeAllNull(tries *root)
{
    int i=0;
    for(i=0;i<=26;i++)
    {
        root->start[i] = NULL;
    }
   return root;
}

/* Insert the given string in to the tries */
tries *insert(tries *root,char *str,int len)
{
    int i=0;
    tries *temp;
    temp = (tries *)malloc(sizeof(tries));
    while(i<len)
    {
        int k = str[i] - 'a';
        temp->start[k] = (node *)malloc(sizeof(struct nodeData));
        temp->start[k]->ch = str[i];
        temp->start[k]->next = NULL;
        if(temp->start[k] == NULL)
        {
            root->start[k] = temp->start[k];
        }
        else{
                root->start[k]->next = temp->start[k];
            }
        i++;

    }
    return root;
}

int main()
{
    int i=0;
    tries *root;
    root = (tries *)malloc(sizeof(node *));
    makeAllNull(root);
    char str[30];
    while(i<5)
    {
        scanf("%s",str);
        root = insert(root,str,strlen(str));
    }
    return 0;
}

最佳答案

if(temp->start[k] == NULL)之后,您的malloc几乎总是错误的,这将导致在此行的指针被取消引用

root->start[k]->next = temp->start[k];

这会破坏记忆。
你是说NULL而不是if(root->start[k] == NULL)

关于c - 尝试:为什么出现malloc错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38909792/

10-12 16:12