'malloc(): memory corruption (fast)

I just started to learn data structure and getting an error when executing

malloc(): memory corruption (fast)

what can be the issue and how to resolve it? I have called the following method in the main method: This is c plus plus code

 struct tnode
    {
        int data;
        struct tnode* left;
        struct tnode* right;
    };
    tnode *root;
    class btree
    {
        public:
            void *insert(int val)
            {
                tnode *newnode, *ptr;
                tnode* temp = new tnode;
                temp->data = val;
                temp->left = NULL;
                temp->right = NULL;
                if (!root)
                root = newnode;
                else
                {
                    ptr = root;
                        while (ptr != NULL)
                        {
                            if (val < ptr->data)            
                            {      
                                if (ptr->left)
                                    ptr = ptr->left;
                                else
                                {
                                    ptr->left = newnode;
                                    break;
                                }
                            }
                            else if (val > ptr->data)
                            {
                                if (ptr->right)
                                    ptr = ptr->right;
                                else
                                {
                                    ptr->right = newnode;
                                    break;
                                }
                            }
                            else
                            {
                                cout << "Duplicate value found in tree.\n";
                                break;
                            }
                        }
                }
            }
};

    int main()
    {
        btree t;
        cout << "Inserting nodes.\n";   
        t.insert(5);
        t.insert(8);
        t.insert(3);
        t.insert(12);
        t.insert(9);
    }


Solution 1:[1]

The reason of the error is not declaring variables in minimal scope where they are used.

void *insert(int val)
{
    tnode *newnode, *ptr;
    tnode* temp = new tnode;
    temp->data = val;
    temp->left = NULL;
    temp->right = NULL;
    if (!root)
    root = newnode;
    //...

As you can see the variable newnode was not initialized but is used in this assignment:

root = newnode;

or in assignments like this:

ptr->left = newnode;

It seems you mean the pointer temp instead of the pointer newnode. At least one of the pointers is redundant.

Pay attention to that the pointer root should be a data member of the class btree including the definition of the structure tnode.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer