I'm implementing a Red Black Tree in C, and when I go to allocate memory for the second node, it gives the error:
sysmalloc: Assertion [...] failed. Aborted (core dumped)
I have already researched and imagined that when I allocate memory for the second node C accesses some area in memory that is already allocated, I suppose it is because the size of struct
is relatively large (8 bytes). Here is the code:
//struct que define os nodes
struct node {
int key;
struct node * left;
struct node * right;
struct node * parent;
char c;
};
typedef struct node Node;
//função que seta os valores do no e retorna
Node* setNode(Node* parent, int value){
Node* new = (Node *) malloc(sizeof(Node*));
new->key = value;
new->parent = parent;
new->left = NULL;
new->right = NULL;
new->c = 'R';
return new;
}
Node* insert(Node* node, Node* parent, int key){
//checks if node is root
if (node == NULL){
//printf("%d\n", key);
node = setNode(parent, key);
//root node is always black
//printf("%d\n", key);
node->c = 'B';
return node;
}
if (key < node->key){
printf("entro aqui\n");
return insert(node->left, node, key);
}
else
return insert(node->right, node, key);
}