Weird behaviour with max in C++

I was trying out (irrelevant) 1372. Longest ZigZag Path in a Binary Tree and I noticed some weird behaviour with C++. 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 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!

Comments (0)