allocating memory for sprintf problem?
the problem is i dunno how long the string will be, so i dunno how much should i allocate for my data...
开发者_StackOverflow社区char *Data = malloc(1024*1024); //???????
sprintf(Data, "%s %s", Data1, Data2);
Data1 and Data2 varies in Size from time to time.... from very very long to very small i was wondering if there is away to use sprintf without allocating memory or something, since the allocated memory could be small sometime..
thanks
Many C runtime libraries (for instance, GNU's glibc and FreeBSD's (and NetBSD's and OpenBSD's and OS X's) libc) provide asprintf
, which combines malloc
and sprintf
into a single call.
char *Data = NULL;
asprintf(&data, "%s %s", Data1, Data2);
Assuming the return value indicates success, enough space has been dynamically allocated for the string, which you should free
when no longer used.
Since data1 and data2 seem to be strings just use strlen() and allocate just the amount of memory you actually need.
char *data = malloc(sizeof(char) * (strlen(data1) + strlen(data2) + 1));
Instead of sprintf()
use snprintf()
which will never overflow the buffer (you tell it how large the buffer is). snprintf()
will also tell you how big the buffer needs to be to avoid truncation of the result.
Note that MSVC doesn't have an snprintf()
but includes the similar _snprintf()
. However that function has at least 2 differences from the standard:
- it might leave the result unterminated
- it won't tell you how large of a buffer you need if it does truncate
If you're using MSVC, you might want to consider using Holger Weiss' snprintf()
implementation (which has a liberal license).
Not possible. You always have to allocate the buffer used by sprintf/printf
The short answer is no.
...But you don't need to allocate every single time your code is run. Depending on whether or not your program is multi threaded, how frequently this section of code is going to be run and how big the biggest possible string you expect is, you are probably better off allocating the a single buffer of "biggest possible size" once and using a safe version of sprintf like snprintf to fill it. (Guards against overruns). Or you could conditionally reallocate the buffer if the size you require exceeds the current buffer size.
For example, if you are using C++ and your code above lives inside a method, you could change your Data allocation line to:
static char *Data = malloc(1024*1024);
The static keyword used within a method promises that your local variable will only be initialized once - the first time the line is invoked and will live beyond the scope of that method call.
If you are using C, then your buffer could be a global (I'm not a fan of this), or you could pass a pointer to a locally scoped Data buffer around. I'm sure real C programmers have better suggestions on how to do this kind of thing.
I know it seems inefficient to just have a giant buffer hanging around that you almost never take full advantage of, but it turns out that for most applications, the overhead of allocation is a much bigger issue than the possibility running out of heap space.
Why not use the strlen functions for the string sizes?
char *buf = malloc(strlen(Data1) + strlen(Data2) + 2); // 2 for space and \0
sprintf(buf, "%s %s", Data1, Data2);
This solves the problem where you need to figure out the size of Data1 and Data2 at runtime.
精彩评论