I written a program in c which creates the root of a tree and adds a node to the left child of the root, but somehow when I try to allocate memory for the child-node I get
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)
even though I am not calling the function free()
.
I'm using the gcc compiler.
main.c
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "functionality.h"
int main(){
BTNode root=NULL,tmp;
root=BTCreate();
BTInsertLeft(root,'c');
return 0;
}
functionality.c
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
BTNode BTInsertLeft(BTNode node,BTItem item){
node->left=malloc(sizeof(BTNode));
BTNode tmp=node->left;
tmp->left=NULL;
tmp->right=NULL;
tmp->item=item;
tmp->parent=node;
return tmp;
}
BTNode BTCreate(){
BTNode root=malloc(sizeof(BTNode));
root->item='a';
root->right=NULL;
root->left=NULL;
root->parent=NULL;
return root;
}
types.h
typedef char BTItem;
struct treenode{
BTItem item;
struct treenode *left;
struct treenode *right;
struct treenode *parent;
};
typedef struct treenode TREENODE;
typedef TREENODE *BTNode;
functionality.h
BTNode BTCreate();
BTNode BTInsertLeft(BTNode node,BTItem item);