I am trying to use the following id function in C++. I would have assumed that I can simply do;
typedef int v3[3];
v3 & id( v3 & in )
{
in[0] = in[1] = in[2] = 1;
return in;
}
int main()
{
v3 & v = id( v );
return 0;
}
However this leads to a segfault with g++ (this is not clear to me). Since I can do the following with scalar-type:
int & zero( int & in )
{
in = 0;
return in;
}
int main()
{
int i = zero( i );
return 0;
}
How can I use id with a single line of code ? I'd like to avoid an ugly solution such as:
int main()
{
v3 v;
v3 & w = id( v );
}
Of course I do not want to use std::vector or std::array (c++11). So I am really interested why my naive solution is actually undefined behavior.
EDIT: std::vector will require dynamic allocation which in my case should be avoid (I need to manipulation thousands of vr3). I cannot use std::array since I am required to stick to c++03. Obviously one could not guess my requirements.