我一直得到错误时运行我的尝试程序。
我的代码是:。out: malloc_c:2372: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) ->大小,0 x1),,((unsigned long) old_end &Pagemask) == 0)' failed.
#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
之后几乎总是假的,这导致对NULL
指针在这一行的解引用
root->start[k]->next = temp->start[k];
这会破坏记忆。
您是指if(root->start[k] == NULL)
而不是if(temp->start[k] == NULL)
吗?