C语言 红黑树(rbtree)map 释放整个map

项目中要使用黑红树map来遍历数组进行加密,C语言不如java一般能自己GC。所以,C语言中要时刻警惕释放问题。

先放github连接 https://github.com/Broadroad/map。我是使用该连接中的rbtree,经验证使用过程中无bug。

但在最后释放整个map的时候遇到了问题,以至于研究很久,查询很多资料,最后解决了释放问题。废话不多说,直接上代码

遍历map数组

    #include "map.h"
    char *url = "http://i.baiwang.com";

    root_t tree = RB_ROOT;
    put(&tree, "AAA", "aaa");
    put(&tree, "BBB", "bbb");
    put(&tree, "CCC", "ccc");
    put(&tree, "path", url);

    //Traversal 遍历map
    map_t *node = NULL;
    char dataString[10000] = "";
    for (node = map_first(&tree); node; node = map_next(&(node->node)))
    {
        strncat(dataString, node->val, strlen(node->val));
    }
    map_destory(&tree);
    //输出dataString
    printf("dataString==%s\n", dataString);

以下为个人修改github项目中map.h的 map_destory方法。可以直接释放map数组

注意:github中作者的test_map.c文件中 释放整个map的方法是错误的。(因为是我之前给作者提供的方法,但是是错误的,哈哈哈哈)

#ifndef _MAP_H
#define _MAP_H

#include "rbtree.h"
#include 
#include 
#include 
#include 

struct map {
    struct rb_node node;
    char *key;
    char *val;
};

typedef struct map map_t;
typedef struct rb_root root_t;
typedef struct rb_node rb_node_t;

map_t *get(root_t *root, char *str) {
   rb_node_t *node = root->rb_node;
   while (node) {
        map_t *data = container_of(node, map_t, node);

        //compare between the key with the keys in map
        int cmp = strcmp(str, data->key);
        if (cmp < 0) {
            node = node->rb_left;
        }else if (cmp > 0) {
            node = node->rb_right;
        }else {
            return data;
        }
   }
   return NULL;
}

int put(root_t *root, char* key, char* val) {
    map_t *data = (map_t*)malloc(sizeof(map_t));
    data->key = (char*)malloc((strlen(key)+1)*sizeof(char));
    strcpy(data->key, key);
    data->val = (char*)malloc((strlen(val)+1)*sizeof(char));
    strcpy(data->val, val);

    rb_node_t **new_node = &(root->rb_node), *parent = NULL;
    while (*new_node) {
        map_t *this_node = container_of(*new_node, map_t, node);
        int result = strcmp(key, this_node->key);
        parent = *new_node;

        if (result < 0) {
            new_node = &((*new_node)->rb_left);
        }else if (result > 0) {
            new_node = &((*new_node)->rb_right);
        }else {
            return 0;
        }
    }

    rb_link_node(&data->node, parent, new_node);
    rb_insert_color(&data->node, root);

    return 1;
}

map_t *map_first(root_t *tree) {
    rb_node_t *node = rb_first(tree);
    return (rb_entry(node, map_t, node));
}

map_t *map_last(root_t *tree) {
    rb_node_t *node = rb_last(tree);
    return (rb_entry(node, map_t, node));
}

map_t *map_next(rb_node_t *node) {
    rb_node_t *next =  rb_next(node);
    return rb_entry(next, map_t, node);
}

map_t *map_prev(rb_node_t *node) {
    rb_node_t *prev =  rb_prev(node);
    return rb_entry(prev, map_t, node);
}

/*
描述:删除整颗红黑树
*/
void map_destory(root_t *root)
{
    rb_node_t *free_node = NULL, *tmp_node = NULL;
    map_t *nodeFree = NULL;

    for(free_node = rb_first(root); free_node; )
    {
        tmp_node = rb_next(free_node);
        nodeFree = rb_entry(free_node, map_t,node);
        rb_erase(free_node, root);
        map_free(nodeFree);
        free_node = tmp_node;
    }
}

void map_free(map_t *node)
{
    if (node != NULL)
    {
        if (node->key != NULL)
        {
            free(node->key);
            node->key = NULL;
            free(node->val);
            node->val = NULL;
        }
        free(node);
        node = NULL;
    }
}

#endif  //_MAP_H

你可能感兴趣的:(C语言 红黑树(rbtree)map 释放整个map)