gpt4 book ai didi

c++ - Visual C++ _snprintf 将 errno 设置为 ERANGE

转载 作者:太空狗 更新时间:2023-10-29 20:57:00 24 4
gpt4 key购买 nike

我刚刚注意到,当我的代码使用 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 had n 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 than n.

至于 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 of errno 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/

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