In C++ the size of an array must be determined at the compile-time. I want to write a simple code in C++, say, to do a naive matrix multiplication with itself (for a matrix that is square in size) and I want to do it for any matrix size. However, I do not know how I can get the matrix sizes or the matrices of various sizes at the run-time in C++.
I have read that there are two types of memory, stack and heap. It seems the size of arrays on stack must be determined at the compile-time while the size of an array on heap can be determined at rune-time.
I also know that Numpy can get the size of an array from the user in Python and create an array in C/C++, so, the way Numpy do this might be the solution.
Does Numpy do compilation after getting the size of an array from the user? or are Numpy arrays created on the heap in C? Basically, how does Numpy avoid this issue?
Edit I changed the question to put the focus on how Numpy resolves the issue I had.
std::vectorrather than a raw array, it can have its size set at runtime. (Usesnew[]under the hood to achieve that.) It would also be a nice idea to use something likestd::mdspanas your function parameter to be able to accept sub-matrices without copying them into new storage, but it's not supported in GCC yet.malloc()ornew[](which are closely related). The point is that you (usually) have to allocate on the heap to be able to set the size at runtime.