gpt4 book ai didi

c++ - 修复了 g++ 4.9.1 奇怪的 "%a"格式行为?

转载 作者:可可西里 更新时间:2023-11-01 16:17:50 34 4
gpt4 key购买 nike

编译器:来自 Nuwen 发行版的 64 位 MinGW G++ 4.9.1,在 Windows 8.1 下。

代码:

#ifdef INCLUDE_IOSTREAM
# include <iostream>
#endif
#include <stdio.h> // ::snprintf
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
#include <stdexcept> // std::exception

#ifdef snprintf
# error snprintf defined as macro
#endif

#ifdef _MSC_VER
auto const snprintf = _snprintf;
#endif

void test( double const value, int const precision)
{
char buffer[34];
snprintf( buffer, sizeof( buffer ), "%.*a", precision, value );
printf( "Hex of %.3f with %2d digits: %s\n", value, precision, buffer );
}

auto main() -> int
{
using namespace std;
try
{
for( int precision = 6; precision <= 8; ++precision )
{
test( 5.0, precision );
}
test( 0.0, 14 );
return EXIT_SUCCESS;
}
catch( exception const& x )
{
fprintf( stderr, "!%s\n", x.what() );
}
return EXIT_FAILURE;
}

与 Visual C++ 配合良好(但 Visual C++ 似乎缺少相反的转换):

H:\dev\test\so\0187>cl /nologo- /? 2>&1 | find /i "ler ver"Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30723 for x86H:\dev\test\so\0187>cl barx.cpp -D INCLUDE_IOSTREAM /Febbarx.cppH:\dev\test\so\0187>bHex 5.000 with  6 digits: 0x1.400000p+2Hex 5.000 with  7 digits: 0x1.4000000p+2Hex 5.000 with  8 digits: 0x1.40000000p+2Hex 0.000 with 14 digits: 0x0.00000000000000p+0H:\dev\test\so\0187>_

<iostream> 时也可以与 g++ 一起正常工作包括:

H:\dev\test\so\0187>g++ --version | find "++"g++ (GCC) 4.9.1H:\dev\test\so\0187>g++ -std=c++11 barx.cppH:\dev\test\so\0187>aHex of 5.000 with  6 digits: 0x1.400000p+2Hex of 5.000 with  7 digits: 0x1.4000000p+2Hex of 5.000 with  8 digits: 0x1.40000000p+2Hex of 0.000 with 14 digits: 0x0.00000000000000p+0H:\dev\test\so\0187>_

奇怪的结果 w/hang<iostream>包括:

H:\dev\test\so\0187>g++ -std=c++11 -D INCLUDE_IOSTREAM barx.cppH:\dev\test\so\0187>aHex of 5.000 with  6 digits: 0xa.000000p-1Hex of 5.000 with  7 digits: 0xa.0000000p-1Hex of 5.000 with  8 digits: 0x0.00000000p-33           ← Weird.^C                                                      ← Hang, Ctrl+CH:\dev\test\so\0187>_

我要求修复或解决方法。

最佳答案

Microsoft 的实现有许多 printf 错误,这些错误默认会影响 MinGW(#377#407 等)。

在所有情况下推荐 appears to be在预处理器中将 __USE_MINGW_ANSI_STDIO 定义为 1 以改为使用 MinGW 自己的符合 ANSI 标准的实现。

据推测,Visual Studio 有自己的内部解决方法来解决底层系统代码中的缺陷。

关于c++ - 修复了 g++ 4.9.1 奇怪的 "%a"格式行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161720/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com