简体   繁体   中英

Eigen Instantiation of Dynamic Matrix Encountering OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG During Compilation

I have a class that uses a dynamic Eigen::Matrix of doubles (ie Eigen::MatrixXd ), that has max rows and cols known at compile time. During use, the dimensions may be less than the maximum. When I compile the code, I get an OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG error. I'm using Eigen 3.2 and VisualStudio 2017 with the default options and flags. This error occurs whether in Debug or Release mode. The Target arch is x64.

Here's the code (simplified):

class MyClass {
public:
  MyClass();
  ~MyClass();
 
private:
  // Create a member that is an Eigen::Matrix of doubles w/ dynamic sizing and max rows and cols 
  // at compile time = 1024.
  Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 1024, 1024 > x;

  // Eigen::MatrixXd x(1024, 1024); // -- FAILS too!
};

What appears to be happening is that during the template expansion, the dynamic nature of the array declaration is ignored and the expansion thinks it is a static array which causes the size checking in Eigen::internal::check_static_allocation_size() to assert. I'm not sure why the dynamic flags/constants are being ignored.

This is shown in the documentation

ABI and storage layout

The table below summarizes the ABI of some possible Matrix instances which is fixed thorough the lifetime of Eigen 3.

...

Matrix<T,Dynamic,Dynamic,0,MaxRows,MaxCols> 

Equivalent C structure

struct {
  T data[MaxRows*MaxCols];  // with (size_t(data)%A(MaxRows*MaxCols*sizeof(T)))==0
  Eigen::Index rows, cols;
 };

Eg, you're specifying dynamic matrix with a constant size container (for speed probably). You should just use Matrix<T,Dynamic,Dynamic> for an allocated dynamic matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM