小编Roo*_*uby的帖子

snprintf手册页示例内存泄漏?

snprintf(3)的Linux手册页给出了以下示例:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

char *
make_message(const char *fmt, ...)
{
    int n;
    int size = 100;     /* Guess we need no more than 100 bytes */
    char *p, *np;
    va_list ap;

    if ((p = malloc(size)) == NULL)
        return NULL;

    while (1) {

        /* Try to print in the allocated space */

        va_start(ap, fmt);
        n = vsnprintf(p, size, fmt, ap);
        va_end(ap);

        /* Check error code */

        if (n < 0)
            return NULL;

        /* If that worked, return …
Run Code Online (Sandbox Code Playgroud)

c linux printf manpage

7
推荐指数
1
解决办法
739
查看次数

标签 统计

c ×1

linux ×1

manpage ×1

printf ×1