gpt4 book ai didi

c - strerror_r 无效

转载 作者:太空狗 更新时间:2023-10-29 15:29:43 25 4
gpt4 key购买 nike

这是一个说明问题的简单程序。

#include <string.h>
#include <iostream>

int main () {
char buf [30];
strerror_r (0, buf, sizeof (buf));

std :: cout << "strerror_r(0): " << buf << std :: endl;
std :: cout << "strerror(0): " << strerror (0) << std :: endl;
}

这是输出。

strerror_r(0): 
strerror(0): Success

为什么buf里什么都没有?

(在 Ubuntu 上用 gcc 编译。)

最佳答案

来自 man strerror_r :

The XSI-compliant version of strerror_r() is provided if: (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE Otherwise, the GNU-specific version is provided.

再往下:

The GNU-specific strerror_r() returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most buflen bytes are stored (the string may be truncated if buflen is too small and errnum is unknown). The string always includes a terminating null byte.

程序必须使用特定于 GNU 的版本并且不填充 buf。我重现了已发布程序的行为,但存储了 strerror_r() 的返回值,它与 buf 的地址不同。

关于c - strerror_r 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16384484/

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