Force the matrix to be heap-allocated

Greetings, and thanks for the great work!

I’m struggling now with the relatively large matrix (in my use-case, it’s 264x264). I’d like to have compile-time checks all around (and the access to Zero::zero() and One::one() initializers), so I’ve typed it with the typenum's U264. But it seems that any statically-known matrix will go to the stack, and this one in particular have happily caused stack overflow from the very beginning. Can I force it to go to the heap without making it dynamically-sized?

Small example to check how I’m doing it now:

use nalgebra::MatrixN;
use num::Zero;
type LEMat = MatrixN<f64, typenum::consts::U264>;

fn main() {
    let _matrix: LEMat = Zero::zero();

Here the problem is reproduced only in debug, of course (in release the array is optimized out), but real code crashes in release too due to the same error:

thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\release\program.exe` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)
1 Like


This is not possible currently. At least one of the dimensions of the matrix must be Dynamic for the internal storage to be heap-allocated.

1 Like

I thought you could do this by setting the buffer during type def to be MatrixVec?

I thought you could do this by setting the buffer during type def to be MatrixVec?

While you can do this, it is not very encouraged because operations involving this matrix and returning a matrix with the same shape (for example martrix * matrix) will produce a stack-allocated matrix. The reason is that the storage type of the result will be automatically deduced from the type parameters for he result matrix dimensions. And it will do so even if matrix uses a VecStorage. So at some point a stack overflow will still happen.