Reputation: 94
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);
Upvotes: 0
Views: 129
Reputation: 781633
You're not allocating enough space in BTCreate()
. sizeof(BTNode)
is the size of a pointer, not the size of a treenode
structure. It should be:
BTNode root = malloc(sizeof(*root));
or
BTNode root = malloc(sizeof(TREENODE));
The general rule is that the type argument to sizeof()
in a malloc()
call should not be the same as the type of the variable you're assigning to. It should be the type that the pointer points to, i.e. it should have one less *
than the variable's type, or you can use *variable
to refer to the value that it points to.
It's also often a bad idea to typedef pointers. See Is it a good idea to typedef pointers?. The name you used, BTNode
, is a good example of the confusion it causes; you call it a "node", but it's not actually a node, it's a pointer to a node. You should use a name like BTNodePtr
, but what's the point of that when you can just say TREENODE *
to be perfectly clear?
Upvotes: 3