[英]Segmentation fault with g++ (when using to_string twice on NaN)
假设我们有以下设置:
# CMakeLists.txt
project(Scripts CXX)
add_executable(${PROJECT_NAME} script.cpp)
// script.cpp
#include <limits>
#include <string>
void func()
{
const double x = std::numeric_limits<double>::quiet_NaN();
std::to_string(x);
std::to_string(x);
}
int main()
{
func();
return 0;
}
当我们使用GNU 7.3.0在 Linux 上以发布模式编译并运行它时,我们得到一个段错误:
-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: .../gcc-7.3.0/bin/g++
-- Check for working CXX compiler: .../gcc-7.3.0/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
====================[ Build | Scripts | Release ]===============================
.../clion-2020.1.1/bin/cmake/linux/bin/cmake --build .../test_scripts/cmake-build-release --target Scripts --
Scanning dependencies of target Scripts
[ 50%] Building CXX object CMakeFiles/Scripts.dir/script.cpp.o
[100%] Linking CXX executable Scripts
[100%] Built target Scripts
.../test_scripts/cmake-build-release/Scripts
Signal: SIGSEGV (Segmentation fault)
当注释掉第二个 to_string(即//std::to_string(x);
)或将x
更改为非 nan 值(即x = 10;
)或在Debug
模式下运行时,则没有段错误。
有什么我做错了,还是这是 g++ 错误?
这是 g++ 错误吗?
是的,这是 7.x 版本中的一个错误:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86274
它已在 gcc 的版本 8 中得到修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.