I'm writing a general purpose templated library for vector and matrix math. I want to be able to initialise a matrix with a 2D array. A matrix consists of an array of column vectors. A vector can take a pointer to a 1D array of initial values. I've tested that this works. But the similar constructor for the matrix causes a sigsegv. As far as I can see, all the array math is fine. From the stack trace, the value for initArray in Vec<4,float>::Vec is bogus. It should be pointing to the first element of one of the inner arrays of the 2D array passed into Mat<4,4,float>::Mat in main.

Here's a copy of the call stack at the time of the error:

And here is the relevent code from those functions:#0 00417483 Math::Vec<4u, float>::Vec(this=0x28fd30, initArray=0x3f800000) (E:/Documents/Prog/mathlib/vec.h:145)

#1 00417292 Math::Mat<4u, 4u, float>::Mat(this=0x28fe90, initArray=0x28fed0) (E:/Documents/Prog/mathlib/mat.h:80)

#2 00401505 main() (E:/Documents/Prog/mathlib/test_main.cpp:16)

main

Matrix constructorCode:typedef Math::Mat<4, 4> Mat4; float mat_init[4][4] = { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }; Mat4 m1((const float**)mat_init); // Error here Mat4 m2((const float**)mat_init);

Vector contructorCode:template <unsigned int X, unsigned int Y, typename T> Mat<X, Y, T>::Mat(const T** initArray) { for (unsigned int i=0; i<X; ++i) cols[i] = Vec<Y, T>(initArray[i]); }

And the interfaces for vectors and matricesCode:template <unsigned int N, typename T> Vec<N, T>::Vec(const T* initArray) { for (unsigned int i=0; i<N; ++i) els[i] = initArray[i]; }

Code:template <unsigned int N, typename T = float> class Vec { public: Vec(); // Init elements to default value of type T Vec(const T* initArray); // Init from array of N elements of type T Vec(const T& initVal); // Init all elements to initVal Vec(const Vec& copy); ~Vec(); Vec& operator= (const Vec& rhs); T& operator[] (unsigned int i); const T& operator[] (unsigned int i) const; // Cast to any other type of vector (any dimensions, any element type) template <unsigned int n, typename t> operator Vec<n, t> () const; private: T els[N]; };Thanks.Code:template <unsigned int X, unsigned int Y, typename T = float> class Mat { public: Mat(); Mat(const T** initArray); // Init by array T [X][Y] Mat(const Vec<Y, T>* initArray); // Init by array of column vectors Mat(const Mat& copy); ~Mat(); Mat& operator= (const Mat& rhs); Vec<Y, T>& operator[] (unsigned int); const Vec<Y, T>& operator[] (unsigned int) const; private: // Store elements as X columns of Y elements of T Vec<Y, T> cols[X]; };