ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The following code works for me. Note that I am not including malloc.h. It's a non-standard header that's not needed in your program. vsnprintf() is also non-standard btw.
Code:
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
static void message(const char *, ...);
int
main(void)
{
message("Hello, %s\n", "World!");
return 0;
}
static void
message(const char *format, ...)
{
va_list args;
int length = 0;
char *buffer = NULL;
va_start(args, format);
length = vsnprintf(NULL, 0, format, args);
assert(length > 0);
++length; /* Make sure we have room for \0 */
buffer = malloc(length); /* sizeof(char) not needed */
vsnprintf(buffer, length, format, args);
va_end(args);
printf("%s", buffer);
free(buffer);
}
One more thing: If the string passed to the message() function doesn't end with a newline you might want to flush the output buffer so you make sure the string is displayed right away.
Tell me where you found that it was okay to pass a NULL pointer as the first argument to vsnprintf() like you did here:
Code:
lLength=vsnprintf(NULL, 0, cpStr, args);
This is most likely evil. And trust me, the last place you want to declare as the broken part of your code is within a libc function. Guaranteed you're just using it incorrectly.
Works fine with newlib's vsnprintf() too.
My manual says:
The number of characters that would have been written into the array, not counting the terminating null character, had count been large enough. It does this even if count is zero; in this case buf can be NULL.
Works fine with newlib's vsnprintf() too.
My manual says:
The number of characters that would have been written into the array, not counting the terminating null character, had count been large enough. It does this even if count is zero; in this case buf can be NULL.
I tried man pages, webpages, and it was still broken. Not to mention that it works on another computer! Anyhow, I just wrote my own, and it works fine. Thanks guys.
Like the original poster, I've been having problems with vsnprintf() producing garbage on a 64--bit machine running Linux (SuSE, not Fedora). I've found a workaround, at least on my system:
Code:
va_list args;
va_start(args, cpStr);
long lLength;
char *cpTemp;
lLength=vsnprintf(NULL, 0, cpStr, args);
cpTemp=(char *)malloc((lLength+1)*sizeof(char));
if (!cpTemp)
{
fprintf(stderr, "Error: Unable to allocate memory for opperation");
}
va_end(args);
va_start(args, cpStr);
if (vsnprintf(cpTemp, lLength, cpStr, args)<0)
{
fprintf(stderr, "Error: Unable to complete opperation");
}
va_end(args);
fprintf(stderr, "%s\n", cpTemp);
free(cpTemp);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.