Question: Weird behaviour with max in C++

Revision en2, by Mohammed-Shoaib, 2020-03-11 05:37:23

I was trying out a problem (irrelevant) on LeetCode1372. Longest ZigZag Path in a Binary Tree. I posted this question on the forum but got no reply. Hence, I am asking the same question here.

I noticed some weird behaviour when using the max function. I took a max in two ways:

Solution 1: max(1 + a, 1 + b)

Solution 2: 1 + max(a, b)

Both of these do exactly the same task of getting the 1 + max. However:

The full code is also given below:

Solution 1
Solution 2

The details for Solution 1 are given below:

  • Runtime Error Message: AddressSanitizer: stack-overflow on address 0x7ffee1685ab8 (pc 0x7f4840414c34 bp 0x7ffee1686300 sp 0x7ffee1685aa0 T0)
  • Failed Input

If you notice, the code for both of them is exactly the same except for line 13. Could someone please tell me why Solution 1 fails? It really makes no sense to me.

Thank you for all your help!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Mohammed-Shoaib 2020-03-11 05:37:23 12
en1 English Mohammed-Shoaib 2020-03-11 05:36:37 2437 Initial revision (published)