在创建结构AVL树

时间:2017-05-29 15:24:40

标签: c malloc memory-address avl-tree

我在C中创建一个AVL树。整个代码适用于一定数量的输入(在这种情况下为字符串),但是当我尝试运行程序时,作为参数,提供一个"字典&#34 ;在某个点上有10万个单词,程序崩溃并发出错误:

    malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && 
    old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse
    (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
    Aborted (core dumped)

我看到了,当我说到某个词时,当我试图将我的结构运行时,程序崩溃了。这是代码:

struct AVLNode
{
   char *words_dic_tree;   
   struct AVLNode* left;   
   struct AVLNode* right; 
   long int height;
};

(...) (主)

while (fscanf(dic, "%s", aux_array_dic) == 1)
{
   if (strcmp(aux_array_dic,"revelar")==0)
   distance = 69;
   word_size = strlen(aux_array_dic)+1;
   words_dic = (char*)malloc(word_size * sizeof(char));
   strncpy(words_dic,aux_array_dic,word_size);
   head_AVL = Insert(head_AVL,words_dic);

}

(...)

struct AVLNode* Insert(struct AVLNode* node, char *words_dic_tree)
{
if (node == NULL){
   return GetNewNode(words_dic_tree);
}
if(strcmp(words_dic_tree,node->words_dic_tree)<0){
   node->left = Insert(node->left, words_dic_tree);
}
else 
   node->right = Insert(node->right,words_dic_tree);

node->height = 1+max(height(node->left),height(node->right));

long int balance = getBalance(node);

if (balance > 1 && strcmp(words_dic_tree,node->left->words_dic_tree) < 0)
   return rightRotate(node);
if (balance < -1 && strcmp(words_dic_tree,node->right->words_dic_tree) > 0)
   return leftRotate(node);
if (balance > 1 && strcmp(words_dic_tree,node->left->words_dic_tree) > 0)
{
   node->left = leftRotate(node->left);
   return rightRotate(node);
}
if (balance < -1 && strcmp(words_dic_tree,node->right->words_dic_tree) < 0)
{
   node->right = rightRotate(node->right);
   return leftRotate(node);
}
return node;
}

struct AVLNode* GetNewNode(char *word)
{
   struct AVLNode* newNode = (struct AVLNode *)malloc(sizeof(struct AVLNode));
   newNode->words_dic_tree = (char *)malloc(sizeof(word));
   strcpy(newNode->words_dic_tree,word);
   newNode->left = NULL;
   newNode->right = NULL;
   newNode->height = 1;
   return newNode;
}

我无法理解我的代码有什么问题导致它崩溃传递了一定数量的单词。它与我的计算机内存或其他东西有关吗?

注意:在我调试时,我看到了,当我尝试在GetNewNode函数中对malloc结构进行malloc时,struct AVL_tree * newnode的地址为&#34;&lt; __ GI ___ libc_malloc + 84&gt;&#34;。我怀疑malloc是出界的

0 个答案:

没有答案