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;
}
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 memorylogbufpoints 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
LOGBUFFERSIZEbytes. 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.