3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

最適化オプションをつけて、autoでハマった例

Last updated at Posted at 2019-05-30

まとめ

自作のプログラム中にこんなバグを作っていたという記録です。発生条件は、

  • auto で変数を定義して、その後にコンストラクタの足し算をしている。
  • -O3 オプションをつけた

便利だからといってauto, autoとつけてはいけないと反省。

コード

eigen.cpp
#include <Eigen/Dense>
#include <iostream>
int main ( int argc, char** argv ) {
  Eigen::Vector3i p0 (-5, -3, -2);
  Eigen::Vector3i p1 (1, 1, 1);
  Eigen::Vector3i p2 = p0 - p1;
  std::cerr<<p2.transpose()<<std::endl;
  auto p3 = Eigen::Vector3i(-5, -3, -2) - Eigen::Vector3i(1, 1, 1); // ここ
  std::cerr<<p3.transpose()<<std::endl;

  auto p4 = p0 - p1;
  std::cerr<<p4.transpose()<<std::endl;

  return 0;
}

##実行結果
実行環境は、Mac (Mojave) + Apple LLVM (10.0.1)

最適化なしだと

$ g++ -I/usr/local/include/eigen3 -std=c++1z  eigen.cpp
$ ./a.out 
-6 -4 -3
-6 -4 -3
-6 -4 -3
$

-O3つけると

$ g++ -I/usr/local/include/eigen3 -std=c++1z -O3 eigen.cpp
$ ./a.out 
-6 -4 -3
      -24         0 188736771
-6 -4 -3
$
3
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?