Sanju
Sanju

Reputation: 115

How to create a large Eigen matrix from a 9222*9222 2d array in c++

I have tried mapping the matrix from the array using

Eigen::Map<Eigen::Matrix<double, 9222, 9222, Eigen::RowMajor>> m(&A[0][0]);

where A is double **A.

A = (double **)malloc((fullsize)*sizeof(double *));         //allocate memory dynamically for matrix A
    for (i = 0; i < fullsize; i++)
        A[i] = (double *)malloc((2 * fullsize)*sizeof(double));

but the program crashes.

I have tried

Eigen::Map<Eigen::Matrix<double, 9222, 9222, Eigen::RowMajor>> m(&A[0][0]);

but the program won't compile.

Upvotes: 1

Views: 496

Answers (2)

Surt
Surt

Reputation: 16089

Try this, Map reinterpret the array as a matrix.

double A[fullsize*fullsize];

Eigen::Map<Eigen::Matrix<double, 9222, 9222, Eigen::RowMajor>> m(&A[0]);

The vector version might work, but you give Map the address of the vector and not the data in it.

Upvotes: 0

The Techel
The Techel

Reputation: 873

A is a pointer pointing to a contiguous field of pointers. These pointers point to a contiguous field of doubles. Thus A doesn't point to a contiguous field of doubles. &A[0] points to the first pointer, &A[0][0] points to the first element in the field of doubles pointed to by &A[0].

Don't use malloc in C++. Have look at this to create a contigous 2D field of doubles: C-style 2D-dynamic array with need to perform only one free() operation

Upvotes: 1

Related Questions