c - 使用二叉搜索树插入泄漏内存

时间:2014-04-14 20:06:04

标签: c memory-leaks

我使用过Valgrind并且无法确定我的代码在insert()和buildName()函数中泄漏内存的位置。 Valgrind说它具有这些功能,但我无法精确指出其中的位置。我对使用代码泄漏内存的概念很新。帮助任何人?干杯!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* Node of tree */
typedef struct Node {
    int order;
    char value;
    struct Node * left;
    struct Node * right;
} Node;

Node * insert(Node * node, char value, int order){
    if(node == NULL){
        / * free(node); */
        node = (Node *)malloc(sizeof(Node));
        node->value = value;
        node->order = order;
        node->left = NULL;
        node->right = NULL;
        return node;
    }
    if(order > node->order){
        node->right = insert(node->right, value, order);
    }
    else if(order < node->order){
        node->left = insert(node->left, value, order);
    }
  return node;
}

char * buildName(const char * old, const char * newpart) {
    char * dot;
    char * buf = NULL;

    dot = strrchr(old, '.');
    if (dot) {
        size_t len;
        len = snprintf(NULL, 0, "%.*s%s.%s",
                  (int)(dot - old) + 1, old, newpart, dot + 1);
        buf = malloc(len + 1);
        if (buf) {
        snprintf(buf, len + 1, "%.*s%s.%s",
                (int)(dot - old) + 1, old, newpart, dot + 1);
        }
    }
    return buf;
}

void decode(Node * root, Node * origRoot, char * bitString, char * filename) {
    FILE * fp;
    int i = 0;

    fp = fopen(filename, "w");

    if (fp) {
        while(bitString[i] != '2'){
            if(bitString[i] == '0'){
                if(root->left == NULL && root->right == NULL) {
                    root = origRoot;
                    i--;
                }
                else root = root->left;             
            }
            else if(bitString[i] == '1'){       
                if(root->right == NULL && root->left == NULL) {
                    root = origRoot;
                    i--;
                }
                else root = root->right;                     
            }
            if(root->value != -1){
                fputc(root->value, fp);
                printf("%c", root->value);
            }
            i++;
        }
    }
    fclose(fp);
}

int main(int argc, char * argv[]){

    FILE * fp;
    Node * root = NULL;
    char bitString[256];
    char * filename;
    int numNodes;
    int value;
    int order;
    int i = 0;
    char c;

    if (argc < 2 || argc > 2) {
        printf("No file specified or too many arguments. Exiting...\n");
        exit(1);
    } 

    fp = fopen(argv[1], "r");

    if(!fp){
        printf("Cound not open command file: %s", argv[1]);
        return 0;
    }

    if(fp){
        fscanf(fp, "%d", &numNodes);

        while(i < numNodes){
            fscanf(fp, "%d", &value);   
            fscanf(fp, "%d", &order);
            root = insert(root, value, order);
            i++;
        }

        i = 0;
        while((c = fgetc(fp)) != EOF){
            bitString[i] = c;
            i++;
        }
    }

    fclose(fp);

    filename = buildName(argv[1], "decoded");

    decode(root, root, bitString, filename);

    return 0;
}

添加空:

void empty(Node * node) {
  node = NULL;
  if(node != NULL){
    Node * left = node->left;
    Node * right = node->right;
    free(node);
    if (left)
        empty(left);
    if (right)
        empty(right);
  }
}

1 个答案:

答案 0 :(得分:0)

我认为你在函数中缺少一个&#34; else&#34;?当您创建一个新节点时,您不会立即将其返回,而是继续前进到下一个节点。这甚至可能导致二进制搜索树无法正确构造。试试这个:

Node * insert(Node * node, char value, int order){
if(node == NULL){
    / * free(node); */
    node = (Node *)malloc(sizeof(Node));
    node->value = value;
    node->order = order;
    node->left = NULL;
    node->right = NULL;
    return node;
}
else if(order > node->order){
    node->right = insert(node->right, value, order);
}
else if(order < node->order){
    node->left = insert(node->left, value, order);
}
return node;
}