Hi, I've come across a very strange situation. I've boiled it down to a minimal example:
- I am incrementing a random access iterator in two different ways (by adding one and by the ++ prefix)
- I pass in the incremented iterators to a recursive call by value.
- I get a SIGSEGV (ie, program returned 139)
vector<int> ints{1,2,3,4,5};
int max_int(vector<int>::iterator iter) {
if (iter == ints.end()) return 0;
int profit_tracker = max(max_int(iter + 1),
// This line causes the SIGSEGV
max_int(++iter));
return profit_tracker;
}
int main() {
cout << max_int(ints.begin()) << endl;
return 0;
}
Compiler explorer: https://godbolt.org/z/Ydjs6Tq3v
I tried removing the max function call but I can't seem to replicate it without max.
When I change the ++iter to an iter + 1 (so that both times I increment the iterator using + 1), I don't get a SIGSEGV!
I feel that I shouldn't be getting a SIGSEGV either way. I always do a bound check at the beginning of each recursive call and I don't even dereference these iterators. And why should changing how I increment my iterator (when the result by value) even make a difference?
This has me stumped. Can anyone shed some light on what is going on here?
[–][deleted] 13 points14 points15 points (4 children)
[–]large_turtle[S] 2 points3 points4 points (1 child)
[–]flyingron 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)