Question

I'm using the Armadillo library in C++ for storing / calculating large matrices. It is my understanding that one should store large arrays / matrices dynamically (on the heap).

Suppose I declare a matrix

mat X;

and set the size to be (say) 500 rows, 500 columns with random entries:

X.randn(500,500);

Does Armadillo store X dynamically (i.e. on the heap) despite not using new or delete.? The reason I ask, is because it seems Armadillo allows me to declare a variable as:

mat::fixed<n_rows, n_cols> 

which, I quote: "is generally faster than dynamic memory allocation, but the size of the matrix can't be changed afterwards (directly or indirectly)".

Regardless of the above -- should I use this:

mat A;
A.set_size(n-1,n-1);  

or this:

mat *A = new mat;
(*A).set_size(n-1,n-1);

where n is between 1000 or 100000 and not known in advance.

Was it helpful?

Solution

Does Armadillo store X dynamically (i.e. on the heap) despite not using new or delete.?

Yes. There will be some form of new or delete in the library code. You just don't notice it from the outside.

The reason I ask, is because it seems Armadillo allows me to declare a variable as (mat::fixed ...)

You'd have to look into the source code to see what's going on exactly here. My guess is that it has some kind of internal logic that decides how to deal with things based on size. You would normally use mat::fixed for small matrices, though.

Following that, you should use

mat A(n-1,n-1);

if you know the size at that point already. In some cases,

mat A;
A.set_size(n-1,n-1);  

might also be okay.

I can't think of a good reason to use your second option with the mat * pointer. First of all, libraries like armadillo handle their memory allocations internally, and developers take great care to get it right. Also, even if the memory code in the library was broken, your idea new mat wouldn't fix it: You would allocate memory for a mat object, but that object is certainly rather small. The big part is probably hidden behind something like a member variable T* data in the class mat, and you cannot influence how this is allocated from the outside.

I initially missed your comment on the size of n. As Mikhail says, dealing with 100000x100000 matrices will require much more care than simply thinking about the way you instantiate them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top