PLo
PLo

Reputation: 1

freed BST but still getting memory leak C

I know there are tons of people asking this question, and I have looked through all the answers on those posts but still doesn't fix my problem. I'm trying to free the Binary search tree in C. I written the code for freeing the memory. Here are the code for Insert, createNode and freeNode:

Insertion

    Node *insertNode(Node *root, int value) {
    /*
        Insertion of node in Binary Search Tree. The new node must be added in the correct subtree (either left or right).
        If the value is less than the root value then it should be insert in the left subtree. If it's bigger then it should be
        on the right.
    */
    if (root == NULL) {
        //if this is the first node then return its value.
        root = createNode(value);
        return root;
    }
    //on the left subtree
    if (value < root->data) {
        //recurse down the left subtree
        root->left = insertNode(root->left, value);
    } else if (value > root->data) {
        //recurse down the right subtree otherwise.
        root->right = insertNode(root->right, value);
    }

    return root;
}

Free tree

void freeSubtree(Node *N) {
   if(N == NULL) {
       return;
   } else{
       freeSubtree(N->right);

       freeSubtree(N->left);
       N->right = NULL;
       N->left = NULL;
   }
       free(N);

}

Create new node

Node *createNode(int value) {
    //allocate space for the node.
    Node *newNode = (Node *) malloc(sizeof(Node));
    newNode->data = value;
    newNode->left = NULL;
    newNode->right = NULL;

    return newNode;
}

I don't know why there is still memory leaks since I have freed all the nodes. I can't see where I went wrong.

Any help would be greatly appreciated!

EDIT Here are the memory leaks reported from valgrind Valgrind memory leak error

Upvotes: 0

Views: 591

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754280

I assembled this code based on what's in the question. I've cleaned up your three functions, added the type definition, headers, a tree printing function, and a main() program. I ran it under Valgrind numerous times, with different configurations in the main() program — different numbers in the multiplications, additions and modulo operations, and different sequences for the tree building, and building one tree instead of three, etc. None of these induced a memory leak.

/* SO 5495-1700 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct Node Node;

struct Node
{
    int data;
    Node *left;
    Node *right;
};

static Node *createNode(int value);
static void freeSubtree(Node *node);
static Node *insertNode(Node *root, int value);

Node *insertNode(Node *root, int value)
{
    if (root == NULL)
        root = createNode(value);
    else if (value < root->data)
        root->left = insertNode(root->left, value);
    else if (value > root->data)
        root->right = insertNode(root->right, value);
    return root;
}

void freeSubtree(Node *N)
{
    if (N == NULL)
        return;
    freeSubtree(N->right);
    freeSubtree(N->left);
    N->right = NULL;
    N->left = NULL;
    free(N);
}

Node *createNode(int value)
{
    Node *newNode = (Node *) malloc(sizeof(Node));
    newNode->data = value;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

static void printValueIndented(int level, int value)
{
    for (int i = 0; i < level; i++)
        fputs("    ", stdout);
    printf("%d\n", value);
}

static void printTree(const char *tag, Node *root, int level)
{
    if (root == NULL)
        return;
    if (level == 0 && tag != NULL)
        printf("%s\n", tag);
    printValueIndented(level, root->data);
    printTree(tag, root->left, level + 1);
    printTree(tag, root->right, level + 1);
}

int main(void)
{
    Node *root = 0;
    srand(time(0));
    for (int i = 0; i < 20; i++)
        root = insertNode(root, i);
    printTree("Sequence", root, 0);
    freeSubtree(root);
    root = 0;
    for (int i = 0; i < 20; i++)
        root = insertNode(root, rand() % 53);
    printTree("Random", root, 0);
    freeSubtree(root);
    root = 0;
    for (int i = 0; i < 20; i++)
        root = insertNode(root, (13 * i + 7) % 47);
    printTree("Computed", root, 0);
    freeSubtree(root);
    return 0;
}

One example run:

Sequence
0
    1
        2
            3
                4
                    5
                        6
                            7
                                8
                                    9
                                        10
                                            11
                                                12
                                                    13
                                                        14
                                                            15
                                                                16
                                                                    17
                                                                        18
                                                                            19
Random
4
    51
        24
            17
                16
                    7
            30
                32
                    31
                    41
                        34
                            36
                                39
                        45
                            43
Computed
7
    4
        1
        6
    20
        12
            9
            17
                14
                19
        33
            25
                22
                30
                    27
            46
                38
                    35
                    43
                        40

It is therefore unclear how your code is triggering a memory leak. If you are still getting the leak, then you need to show the exact code that generates it (create an MCVE — Minimal, Complete, Verifiable Example) and show the Valgrind output in your (updated) question.

Upvotes: 1

Related Questions