So here is some sample code:
#include<valarray>
int main()
{
std::valarray<double> vec(10);
for (size_t i=0; i<11; i++)
vec[i] = 1.0;
std::valarray<double> vec2(vec);
return 0;
}
So, in a situation similar to this one, I am segfauting, but not in the loop, rather in the constructor call after the loop. Question is, is this behavior expected generally, or is it unique to valarray, or something else? The strange thing is, even if I re-write the code to construct vec2 based on vec.size() instead of vec itself, the segfault still happens at the constructor for vec2. This is even when the debugger is showing a correct size (10) for vec.size().
I spent some considerable time looking for a bug this afternoon, based on this type of mistake. Just hoping to get some philosophical satisfaction out of it!