设置大型特征矢量Xd时出现叮当错误



我有一个函数,它所做的只是

Eigen::VectorXd x(%s);
x << %s;

其中第一个 %s 是大小,第二个是输入(动态设置我的向量(。当我在"小"输入(> 4000 个参数(上运行它时,一切都很好。但是当我在较大的版本上执行此操作时,我无法编译并且我得到

clang: error: unable to execute command: Illegal instruction: 4
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
clang: note: diagnostic msg: PLEASE submit a bug report to http://developer.apple.com/bugreporter/ and include the crash backtrace, preprocessed source, and associated run script.
clang: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /var/folders/jc/nh9bfd2j5_q4w0x2mbq02svc0000gq/T/wenzel-f181fc.cpp
clang: note: diagnostic msg: /var/folders/jc/nh9bfd2j5_q4w0x2mbq02svc0000gq/T/wenzel-f181fc.sh
clang: note: diagnostic msg: Crash backtrace is located in
clang: note: diagnostic msg: /Users/ipq500/Library/Logs/DiagnosticReports/clang_<YYYY-MM-DD-HHMMSS>_<hostname>.crash
clang: note: diagnostic msg: (choose the .crash file that corresponds to your crash)
clang: note: diagnostic msg:
********************

我已经看到这可能是一个XCode问题,但想知道可能会发生什么。我在这里完全不知所措。

我假设你尝试做的事情是这样的

Eigen::VectorXd x(4000);
x << 0, 1, 2, 3, /* many more values */ 3999;

这是通过重载<<,运算符来实现的,即语法等效于以下内容:

operator,( /* many more calls ... */
  operator,(operator,(operator,(operator<<(x,0), 1), 2), 3)
           /* ... */, 3999 );

对于编译器来说,这确实很难翻译,因为您有一个 4000 深度的调用堆栈(即使这会被内联,但在编译时这可能会触发一些限制(。

使用 C++11 和开发分支,您可以尝试以下操作(不确定该语法的编译器限制(:

Eigen::VectorXd x{ {0, 1, 2, 3, /* ... */ 3999} };

如果这不起作用,请尝试以下替代方法(兼容C++03(:

static const x_data[4000] = {0,1,2, /* ... */, 3999}; // ideally this should be aligned
Eigen::Map<Eigen::VectorXd> x(x_data, 4000);

或者,如果您有二进制形式的数据(例如,在单独的文件中(,则在运行时mmap该文件并对该数据创建Eigen::Map

最新更新