leftCols 结果分配给同一个矩阵会导致 Segmentation fault

leftCols result assignment to the same matrix causes Segmentation fault

看下面代码

#include "Eigen/Dense"
#include <iostream>
using namespace Eigen;

int main(int argc, char**) {

    MatrixXf A = MatrixXf::Random(4096, 4096);
    MatrixXf B = A.leftCols(1000); // <-- works fine
    std::cout << "--------" << std::endl;
    A = A.leftCols(1000); // <-- SegFault
}

将leftCols的结果赋值给同一个矩阵有什么问题吗?

GCC - 4.8.4(未使用空间标志)
Ubuntu - 14.04
本征 - 3.3.4

这是一个 aliasing issue, in operator= the destination A is first resized, and then the expression A.leftCols(1000) becomes invalid. You need to call conservativeResize:

A.conservativeResize(NoChange,1000);

或者介绍一个临时的:

A = A.leftCols(1000).eval();