0

So, I have made a function to reverse a linked list using recursion. My reverse function is working perfectly, but when I try to run asan on my executable, it is giving me a stack-overflow error at a particular address in the reverse function. I have tried everything to fix it. I tried to make all the variables in the stack NULL after their work is done. I have de-allocated all the memory from the heap. I ran valgrind on the file and it tells me all the memory has been freed and there is no leak. What do I do?

AddressSanitizer: stack-overflow

P.S. I am using clang++ compiler in C++.

5
  • What kind of bug? Commented Sep 25, 2016 at 21:14
  • What else do you need to help me because I did not allocate large memory on the stack and my recursion definitely ends. I used gdb to check for that. Commented Sep 25, 2016 at 21:19
  • 1
    stackoverflow.com/help/how-to-ask Commented Sep 25, 2016 at 21:22
  • I think I got it. ASAN always gives you stack-overflow on deeply recursive functions. There is no bug in the program. Thank you anyways. Commented Sep 25, 2016 at 21:28
  • Can you change the code to be tail-recursive? Commented Sep 25, 2016 at 21:41

1 Answer 1

9

ASAN increases your memory requirements since it inserts sentinel values on the stack. So it's possible you aren't hitting the stack limit normally but you are now with ASAN enabled, since each recursive call is now using more stack space than it was before.

The best thing to do in C++ is to not recurse so deeply, but you can also increase your process's stack limits, e.g. in bash:

ulimit -s unlimited

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.