在ARM芯片上,使用std::vector 的 at()时,用 try catch 捕获异常,会导致性能降低或者 crash 吗?
示例代码:
int main()
{
std::vector<int> data = {1, 2, 4, 5, 5, 6};
// Set element 1
data.at(1) = 88;
// Read element 2
std::cout << "Element at index 2 has value " << data.at(2) << '\n';
std::cout << "data size = " << data.size() << '\n';
try
{
// Set element 6, where the index is determined at runtime
data.at(runtime_six()) = 666;
}
catch (std::out_of_range const& exc)
{
std::cout << exc.what() << '\n';
}
return 0;
}
线下长时间自测无法复现(性能降低、crash),但是线上会低概率偶现 crash(从堆栈无法分析到原因,如果去掉 try catch 则不会 crash)。
这段是c++文档上的示例代码,不管在什么平台上都是确定的行为,不会crash。
线上的话,vector不是线程安全的数据结构,考虑是否由并发导致的问题。