C 中的可变参数函数
1)为什么 /* test1 */
块下的代码不打印任何内容,但 /* test2 */
下的代码打印正确?
2) 如何在 /* test 1 */
代码块中使用 va_arg(va, char*)
。
void rdfDBG(int dbglevel, const char *fmt, ...) {
va_list va;
#if 1 /* test 1 */
char* logbuf;
if ((logbuf = calloc(0x0, LOGBUFFERSIZE))== NULL)
fprintf(stderr, "out of memory\n"); /* 1. Is there a better way instead of using fprintf? */
va_start(va, fmt);
(void) vsnprintf(logbuf, strlen(logbuf), fmt, va); /* 2. print nothing */
va_end(va);
free(logbuf);
#endif
#if 0 /* test 2 */
va_start(va, fmt);
vprintf(fmt, va); /* 2. print "pinfile pings6.txt" */
va_end(va);
#endif
}
int ptInitialize(){
char* pinfile;
pinfile = "pings6.txt";
rdfDBG(kINFO, "pinfile %s\n", pinfile);
return 0;
}
1) Why code under /* test1 */
chunk do not print out anything, but code under /* test2 */
print correctly?
2) How to use va_arg(va, char*)
in /* test 1 */
code chunk.
void rdfDBG(int dbglevel, const char *fmt, ...) {
va_list va;
#if 1 /* test 1 */
char* logbuf;
if ((logbuf = calloc(0x0, LOGBUFFERSIZE))== NULL)
fprintf(stderr, "out of memory\n"); /* 1. Is there a better way instead of using fprintf? */
va_start(va, fmt);
(void) vsnprintf(logbuf, strlen(logbuf), fmt, va); /* 2. print nothing */
va_end(va);
free(logbuf);
#endif
#if 0 /* test 2 */
va_start(va, fmt);
vprintf(fmt, va); /* 2. print "pinfile pings6.txt" */
va_end(va);
#endif
}
int ptInitialize(){
char* pinfile;
pinfile = "pings6.txt";
rdfDBG(kINFO, "pinfile %s\n", pinfile);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/* test 1 */
下的代码不会打印任何内容,因为vsnprint()
函数不会打印到 stdout;它只是将其输出写入提供的缓冲区。不过,如果代码没有崩溃那就纯属运气了,因为这一行:
实际上告诉
calloc()
分配 0 字节的内存。因此,我认为不能保证logbuf
指向的内存也归零——因此,您的缓冲区不仅为零字节长,而且调用strlen()
它可能会崩溃或给你一个无效的结果。另外,
vsnprint()
的第二个参数应该是缓冲区的大小,即您为logbuf
分配的大小,而不是任何字符串的长度已经在其中了;这是为了限制写入缓冲区的字节数,以避免缓冲区溢出。因此,为了让一切正常工作,您需要更改:
..to
....为 1 项
LOGBUFFERSIZE
字节分配空间。并且还要更改:..to..
..将缓冲区的大小传递给它并删除无用的
(void)
强制转换。并且还要在其后面添加一行打印logbuf
,如:。
The code under
/* test 1 */
doesn't print anything because thevsnprint()
function doesn't print to stdout; it just writes its output into the provided buffer.It's pure luck if the code didn't crash, though, because the line:
actually tells
calloc()
to allocate 0 bytes of memory. Because of this, I don't think it's guaranteed that the memorylogbuf
points at is zeroed either -- so not only is your buffer zero bytes long, but callingstrlen()
on it could crash or give you an invalid result.Also, the second argument to
vsnprint()
is supposed to be the size of the buffer, that is the size you've allocated forlogbuf
, not the length of any string already in it; it's to limit the number of bytes written to the buffer to avoid a buffer overrun.So to get everything working, you need to change:
..to..
..to allocate space for 1 item of
LOGBUFFERSIZE
bytes. And also change:..to..
..to pass it the size of your buffer and remove the useless
(void)
cast. And also add a line to printlogbuf
, such as:after it.
这:
永远不会格式化任何内容,因为
logbuf
全部为零(已由calloc
分配,因此strlen
将返回零,这使得 vsnprintf永远不要格式化任何东西,因为你告诉它最多打印零个字符。This:
Will never format anything, because
logbuf
is all zeros (having been allocated bycalloc
, and sostrlen
will return zero, which makes vsnprintf never format anything because you told it to print at most zero characters.