c - snprintf 的输出是否保证在 Visual Studio 2015 中以 null 终止?

标签 c visual-studio-2015 c99

我正在使用我的前任使用的 C 代码库:

#ifdef _MSC_VER
  // Map to equivalent function
  #define snprintf sprintf_s
#endif

代码库需要在 Linux (gcc/clang)、OSX (gcc/clang) 和 Windows (VS) 上编译。我第一次尝试在 Visual Studio 2015 中编译代码。以前使用的是 Visual Studio 2010。我遇到了描述的错误 here并且能够使用已接受答案中的线索进行编译:

#ifdef _MSC_VER
  #if _MSC_VER<1900
    #define snprintf sprintf_s
  #endif
#endif

该项目现在使用 Visual Studio 2015 和 clang 在 OSX 上进行编译。但是,我担心 sprintf_s 中的声明文档:

与 snprintf 不同,sprintf_s 保证缓冲区将以 null 终止(除非缓冲区大小为零)。

如果 Visual Studio 包含 snprintf 的 C99 兼容版本,是否应该保证缓冲区以 null 终止?

我写了一个小程序来评估行为。

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv) {
    char buf[5];
    snprintf(buf, sizeof(buf), "abcdef");
    printf("buffer: %s\n", buf);
    printf("size of buffer: %lu\n", strlen(buf));
    printf("last character a null terminator?: %s\n",
        (strcmp(&buf[4], "\0") == 0) ? "yes" : "no");
    return 0;
}

我使用 Visual Studio 2015 在 OSX 和 Windows 上构建并运行了该程序。

在 OSX 上,输出是:

~$c99 main.c
~$./a.out 
buffer: abcd
size of buffer: 4
last character a null terminator?: yes

在带有 Visual Studio 2015 的 Windows 7 上,输出是

> printf_evaluation.exe
buffer: abcd
size of buffer: 4
last character a null terminator?: yes

这不是表明输出实际上是空终止的,并暗示 MSDN 文档不正确吗?我的例子太琐碎了吗?在 Visual Studio 2015 中,snprintf 的输出可能不会以 NULL 终止吗?

最佳答案

snprintf()只要它的第二个参数大于零,它就总是 nul 终止缓冲区。所以是的,MSDN 文档是错误的。

From C11 standard, snprintf():

The snprintf function is equivalent to fprintf, except that the output is written into an array (specified by argument s) rather than to a stream. If n is zero, nothing is written, and s may be a null pointer. Otherwise, output characters beyond the n-1st are discarded rather than being written to the array, and a null character is written at the end of the characters actually written into the array. If copying takes place between objects that overlap, the behavior is undefined.

(强调我的)。

关于c - snprintf 的输出是否保证在 Visual Studio 2015 中以 null 终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912145/

相关文章:

c++ - 二维 vector 大小的 Visual Studio 2015 断点条件

visual-studio - 无需 roslyn 即可发布网站

c - 在 C 中使用后增量时返回变量

c - C99 是否强制要求 `int64_t` 类型始终可用?

c++ - Makefile 因 C++ 编译而失败

c - %c specifier char limit capped in C,如何避免这种情况?

c++ - OpenCv,将 4x4 cv::Mat 乘以 cv::Point3f 的最佳方法是什么?

c - ANSI C 和 C99 文字前缀和后缀列表

c++ - UNIX:UNIX 中的堆栈大小 (ulimit -s) 应该是多少?

c - 为什么我会收到断言失败的消息?