Incompréhension : free(): invalid pointer Abandon (core dumped)
Bonjour à tous >< ! j'aimerais que quelqu'un m'explique pourquoi j'ai cette erreur dans mon terminal ! (free(): invalid pointer Abandon (core dumped))
pourtant j'estime avoir fait les choses correctement. Merci de votre aide ! je vous donne mon code si-dessous :
Mon main (tests unitaires)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include "./red-black-tree.h"
int main(int argc, char const *argv[])
{
int data = 0;
Node racine = create_node(&data, sizeof(data));
set_color(racine, BLACK);
printf("data = %p", get_data(racine));
destroy_node(racine);
return EXIT_SUCCESS;
} |
Mon .h si besoin
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#ifndef _RED_BLACK_TREE_H_
#define _RED_BLACK_TREE_H_
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
typedef struct _Node *Node;
// racine de l'arbre
Node root = NULL;
typedef enum
{
RED,
BLACK
} COLOR;
struct _Node
{
void *data;
Node left, right, parent;
COLOR color;
};
/*** Getters and Setters ***/
COLOR get_color(Node node);
bool set_color(Node node, COLOR color);
void *get_data(Node node);
Node get_left(Node node);
Node get_right(Node node);
Node get_parent(Node node);
/*** Functions ***/
void create_tree(); // je n'ai pas encore fait
Node create_node(const void *data, size_t data_size);
void destroy_node(Node node);
void destroy_tree(); // je n'ai pas encore fait
Node binary_search_tree(Node n1, Node n2);
void right_rotate(Node node);
void left_rotate(Node node);
void fix_up(Node root, Node node);
void swapping_colors(Node n1, Node n2);
void inorder(Node node);
#endif /* _RED_BLACK_TREE_H_ */ |
et enfin mes fonctions
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
Node create_node(const void *data, size_t data_size)
{
Node node = NULL;
node = (Node)malloc(sizeof(Node));
node->data = malloc(data_size);
assert(node != NULL);
assert(node->data != NULL);
if (node && node->data)
{
node->right = node->left = node->parent = NULL;
set_color(node, RED);
memcpy(node->data, data, data_size);
}
return node;
}
bool set_color(Node node, COLOR color)
{
assert(node != NULL);
if (node)
{
node->color = color;
return true;
}
return false;
}
void *get_data(Node node)
{
assert(node != NULL);
if (node)
{
return node->data;
}
return NULL;
} |
aucune erreurs à la compilation, merci d'avance ;) ! Ah oui j'allais oublié j'ai utilisé l'utilitaire valgrind et il me dit que dans ma fonction create_node() à la ligne 12 dans ce cas il y avait une erreur peut-être parce que je met right, left et parent à NULL et il n'ame pas trop :/