- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我刚刚注意到,当我的代码使用 Visual C++ 2013 编译时,当目标缓冲区太小时,_snprintf 函数将 errno 设置为 ERANGE(结果太大)。这似乎没有记录在案: https://msdn.microsoft.com/en-us/library/2ts7cx93.aspx
我希望返回值为 -1(代码也是如此),但不会对 errno 进行任何干预。这种行为是否记录在其他地方,是否可能是最近的变化? C++11 标准(我相信它添加了 snprintf)对此有什么要说的吗?
最佳答案
Does the C++11 standard (which I believe adds snprintf) have anything to say about this?
是的,您描述的行为对于 snprintf
是正确的。C标准后(C++标准简称):
(7.21.6.5.3.) The
snprintf
function returns the number of characters that would have been written hadn
been sufficiently large, not counting the terminating null character, or a negative value if an encoding error occurred. Thus, the null-terminated output has been completely written if and only if the returned value is nonnegative and less thann
.
至于 errno
- 函数描述没有说明,所以适用:
(7.5.3.) The value of
errno
may be set to nonzero by a library function call whether or not there is an error, provided the use oferrno
is not documented in the description of the function in this International Standard.
关于c++ - Visual C++ _snprintf 将 errno 设置为 ERANGE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32048886/
在 Visual Studio 2013 中,我有以下测试代码: #include #include #include #include int main ( int argc, const
这个问题在这里已经有了答案: Correct usage of strtol (5 个答案) 关闭 2 年前。 我一直在尝试使用 strtol 将 char 数组正确转换为 long,检查是否存在上
errno 值 ERANGE 被 POSIX 记录为 Result too large. 和EOVERFLOW被记录为 Value too large to be stored in data typ
此程序在第一个命令行参数上调用 strtod() 并打印返回值: #include #include #include #include int main(int argc, char **a
我正在尝试使用 errno 来检测我是否执行了导致溢出的操作。然而,尽管我编写了一个故意溢出的函数,errno == ERANGE 是错误的。这是怎么回事? 代码如下: #include #incl
我刚刚注意到,当我的代码使用 Visual C++ 2013 编译时,当目标缓冲区太小时,_snprintf 函数将 errno 设置为 ERANGE(结果太大)。这似乎没有记录在案: https:/
我很困惑 errno 没有被设置为 ERANGE之后 strtod()尝试将具有巨大实数的字符串转换为双浮点数。 这是我运行代码时发生的情况: Enter a real number: 5656565
我是一名优秀的程序员,十分优秀!