0

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
Martian
  • 94
  • 1
  • 9
  • OT: when calling any of the heap allocation functions: `malloc()` `calloc()` `realloc()`, always check (!=NULL) the returned value to assure the operation was successful. If not successful, then call `perror( "your error message" )` to output to `stderr`, both your error message and the text reason the system thinks the error occurred – user3629249 Apr 07 '19 at 18:59
  • OT: regarding: `typedef TREENODE *BTNode;` it is a very poor programming practice to hide a pointer in a typedef statement – user3629249 Apr 07 '19 at 19:01

1 Answers1

3

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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yes I see, I should be more carefull about the size I am allocating when I am using malloc, and you are right about the typedef too, the problem was that with BTNode I had forgot that it was a pointer and therefore I was allocating the size of the pointer not the size of the struct. Thanks a lot for the help. – Martian Apr 05 '19 at 20:37
  • Typedef'ing pointers makes it easy to make that mistake. Don't do it. – Barmar Apr 05 '19 at 20:40