g++的分段错误(在NaN上使用to_string两次时)
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;
}
当我们编译&在Linux上使用GNU7.3.0在Release模式下运行此程序,我们会得到一个segfault:
-- 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
模式下运行时,则不存在segfault。
是我做错了什么,还是这是一个g++错误?
这是g++错误吗?
是的,这是7.x版本中的一个错误:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86274
它在gcc的版本8中得到了修复。
相关文章:
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 使用词法强制转换在'string to double'中设置双精度变量的精度
- std::string to std::regex
- 如何 SWIG std::string& to C# ref string
- jni String to *char and java.lang.UnsatisfiedLinkError:
- 如何设计一个简单的 std::string-to-boost::p osix_time::p time 解析库
- C++ std::string to Ruby VALUE
- C++ CLI System.String^ to MFC LPCTSTR
- 使用C++中的String To Class Lookup表来实例化类
- 创建新文件时std::string to const char*错误
- 有没有办法"convert string to vector<int>"?
- std::string to LPOLESTR
- C++ stringstream string to function(test, char *test2)
- std::vector<std::string> to char* 数组
- C++: C string to std:string
- 施放一个提升::日志::表达式::attr< std::string > to std::string
- std::string to LPBYTE and RegEnumValueA
- 不能将convert std::string to LPWSTR放入函数中
- vector string to string (or c_string) to lpszString