2

I'm using Eigen lib, version 3.2.7 on Windows machine.

I got weird results when I ran the following code:

auto a = Eigen::Array4i{ 95,95,95,95 }-Eigen::Array4i{ 0,1,0,1 };
Eigen::Array4i b = Eigen::Array4i{ 95,95,95,95 }-Eigen::Array4i{ 0,1,0,1 };
std::cout << a;
std::cout << b;

instead of printing the same vector twice, I got the following result:

[0; 0; 0; 0][95; 94; 95; 94]

Interestingly, this issue appears on Release mode only. The output for the Debug mode case is correct.

Does anyone have an explanation for this?

Thanks!

ibezito
  • 5,782
  • 2
  • 22
  • 46
  • 1
    It may be due to eigen's expression templates delaying the evaluation. i.e. `a` could just be an un-evaluated expression instead of some concrete result. Can you check the type of `a`? https://eigen.tuxfamily.org/dox/group__TutorialMatrixArithmetic.html – sgarizvi Nov 28 '16 at 11:54

1 Answers1

2
auto a = Eigen::Array4i{ 95,95,95,95 }-Eigen::Array4i{ 0,1,0,1 };

a is an expression type. The temporary Eigen::Array4i{ 95,95,95,95 }-Eigen::Array4i{ 0,1,0,1 } "dies" (is released) after that line in Release mode, but lives on for a while in Debug. You can force the evaluation of the expression by using .eval():

auto c = (Eigen::Array4i{ 95,95,95,95 }-Eigen::Array4i{ 0,1,0,1 }).eval();
Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
  • 1
    To be more precise, it is not the expression `-` that dies, but its two nested temporary arrays `Array4i` that are nested by references in the expression representing the `-` operation. – ggael Nov 28 '16 at 22:02