2

I was doing a question on leetcode which involves convertting a binary tree into a linked list.

Here is the link to the question : https://leetcode.com/problems/flatten-binary-tree-to-linked-list/

My code:

class Solution {
public:
    void convert(TreeNode* root) {
        if (root == NULL) {
            return;
        }
        if (root->left == NULL && root->right == NULL) {
            return;
        }
        
        convert(root->left);
        convert(root->right);
        
        TreeNode* rgt = root->right;
        root->right = root->left;
        root->left = NULL;
        
        TreeNode* temp = root->right;
        if (temp == NULL) {
            root->right = rgt;
        }
        else {
            while (temp->right != NULL) {
                temp = temp->right;
            }
            temp->right = rgt;    
        }
        return;
    }
    
    
    void flatten(TreeNode* root) {
        convert(root);
    }
};

I keep on getting the error saying: ==31==ERROR: AddressSanitizer: heap-use-after-free on address 0x603000000048 at pc 0x000000372a98 bp 0x7ffc8e114ef0 sp 0x7ffc8e114ee8 READ of size 8 at 0x603000000048 thread T0

How can i fix this issue?

  • 2
    As there are no `new` / `delete` or `malloc` / `free` in the above code BUT it looks like it's using heap allocated data please post a [mcve]. – Richard Critten Jul 22 '22 at 07:52
  • Welcome! Please take the [tour] and read [ask]. Then, extract a [mcve] from your code, so everyone can just compile and run the code. Chances are that you will find the error yourself this way, which is also why it is mandatory here. – Ulrich Eckhardt Jul 22 '22 at 07:52
  • Whatever you have been told, sites like leetcode are *not* any kind of learning or teaching resources. There are a couple of things in the show code that shows you have used that (or similar) site to learn C++. For example the use of the C macro `NULL` for null pointers, or the last unnecessary `return` statement in the `convert` function. If you really want to learn C++, don't use such sites, and invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jul 22 '22 at 07:53
  • Also, there's no better way to be able to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs than building (with extra warnings enabled, treated as errors) and running a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) locally on your own system. If you build with debug information enabled, then you can use the debugger to catch the problem, and you'll be shown exactly where in your code it happens. – Some programmer dude Jul 22 '22 at 07:54
  • Use nullptr i.o. NULL, make sure ALL your pointers are initialized to a valid value. But above all learn to use a debugger, probably you are trying to dereference an invalid pointer. And please if you are learning C++, stay away from leetcode (your future self will thank you). For allocation of memory forget about malloc/free and new/delete and use std::make_unique – Pepijn Kramer Jul 22 '22 at 07:55
  • Can't see anything wrong with the code and if leetcode is to be believed `TreeNode` does not have a destructor, so the error is confusing. – john Jul 22 '22 at 08:18
  • You may get better feedback by asking this question on the Leetcode forums. – Eljay Jul 22 '22 at 11:55

0 Answers0