- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是一个说明问题的简单程序。
#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/
我是错误处理的新手;在我的代码中,我需要测试函数的返回值并在发生错误时打印错误描述。 为了保持代码线程安全,我必须使用 strerror_r,但我有一些难以使用的地方。在以下代码中,发生了错误编号 2
这段代码安全吗: strerror_r(errcode,buffer,length); printf("Error: %s",buffer); 也就是说,如果缓冲区太小,我可以相信缓冲区是空终止的吗?
这是一个说明问题的简单程序。 #include #include int main () { char buf [30]; strerror_r (0, buf, sizeof (
OpenGroup POSIX.1-2001 定义 strerror_r , The Linux Standard Base Core Specification 3.1 也是如此.但是我找不到对错误
char *strerror_r(int errnum, char *buf, size_t buflen); 这些 buf/buflen 参数有什么用? 空缓冲区就像一个魅力: char* buf
我在这样的代码中为线程安全的 strerror_r 提供了一个 C++ 包装器: struct MyErrno {}; std::ostream& operator<<(std::stream& os
我在 Linux 中使用 libc 中的 strerror_r 函数。我用 0 作为缓冲区指针和缓冲区大小参数来调用它,它返回一个指向带有错误消息的字符串的指针。 我复制了一个字符串以供将来使用,但是
Android ndk-9c 似乎与 strerror_r 声明的标准相矛盾。 Linux 手册页显示 strerror_r 声明如下: int strerror_r(int errnum, char
VxWorks 提供了一个只接受两个参数的 strerror_r 版本。 STATUS strerror_r ( int errcode, /* error number */ cha
在测试期间,我有一个模拟对象,它设置了 errno = ETIMEDOUT; 我正在测试的对象看到错误并调用 strerror_r取回错误字符串: if (ret) { if (ret ==
我看到 strerror_r (...) Visual C++ 2008 不再支持 API,可能是因为线程安全问题。我想在我的程序中使用类似的功能。是否有任何其他 winapi 与 strerror_
我正在使用 strerror_r在日志记录辅助函数中。如手册页所述,此函数有两个版本。 POSIX 版本返回一个 int。 GNU 版本返回一个字符串 (char*)。 因此,为了让我的 C++ 代码
我如何获得 POSIX strerror_r而不是 GNU 版本? 我正在 Ubuntu 8.04 上使用 g++ 和 glibc 版本 2.7(基于中的内容)进行编译。 编辑 在上面的手册页上说:
这是我的程序。 #include #include #include #include int main() { char errbuf[256]; errno
我有一个 C 程序,它在 Linux 下编译和运行良好,没有任何警告,但是当试图在 SunOS 上编译它时,我收到以下警告: test.c: In function `my_function': te
在构建 OpenSSL-1.1.0d 时出现以下错误。 ./libcrypto.a(o_str.o):o_str.c:(.text+0x5cb): undefined reference to `st
我是一名优秀的程序员,十分优秀!