<p>In the application that I am working on, the logging facility makes use of <code>sprintf</code> to format the text that gets written to file. So, something like:</p>

<pre><code>char buffer[512];
sprintf(buffer, ... );
</code></pre>

<p>This sometimes causes problems when the message that gets sent in becomes too big for the manually allocated buffer.</p>

<p>Is there a way to get <code>sprintf</code> behaviour without having to manually allocate memory like this? </p>

<p>EDIT: while <code>sprintf</code> is a C operation, I'm looking for C++ type solutions (if there are any!) for me to get this sort of behaviour...</p>

"the logging facility makes use of sprintf to format the text that gets written to file"
```
fprintf()
```
does not impose any size limit. If you can write the text directly to file, do so!
I assume there is some intermediate processing step, however. If you know how much space you need, you can use
```
malloc()
```
to allocate that much space.
One technique at times like these is to allocate a reasonable-size buffer (that will be large enough 99% of the time) and if it's not big enough, break the data into chunks that you process one by one.