I have this program:
#include <iostream>
#include <vector>
int foo(std::vector<int> bar) {
if (bar[0] == 0)
return bar[0];
else
{
bar[0] -= 1;
return foo(bar);
}
}
int main()
{
std::vector<int> bar {9999999};
std::cout << foo(bar);
}
Compile with x86-64 gcc 12.2 using the flag -O3 (here), it exit with code 139:
Program returned: 139
which means my program has a segmentation fault.
So is my program undefined behaviour, or that gcc fails to optimize my tail recursion?
int foo(std::vector<int> bar)->int foo(std::vector<int> &bar)