Code:
#include <valarray>
#include <iostream>
using namespace std;
int main()
{
valarray<int> v0(2, 4);
valarray<int> v1;
v1 = v0;
cout << "v0.size: " << v0.size() << endl;
cout << "v1.size: " << v1.size() << endl;
cout << "v0[0]: " << v0[0] << endl;
cout << "v1[0]: " << v1[0] << endl;
}
Output:
v0.size: 4
v1.size: 0
v0[0]: 2
Segmentation fault
For the assignment:
v1 = v0;
I would think the constructor:
valarray<T>& operator=( const valarray<T>& other );
should be used and according to the documentation, I believe v1 should be resized and the contents of v0 copied into it, element for element. So what's actually happening?
$ g++ --version
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11)
cout
. – Barbituratevalarray
is even weirder than I thought it was. Anyway, I'm unfortunate enough to be stuck on the same gcc version at work, and I can reproduce the behavior in the question. – Psycho