I have a bunch of printf()s that correctly print a quite complex string that I have to build.

The problem is that I need to store that string in a variable (the result of all those printf()s together for sending them via a socket. I'm quite sure I need them to be sent at once - but I'll let a small window open if you want to convince me that's not true.

What's the best way to achieve that?

The string length is REALLY variable. I've heard of sprintf() and realloc(), and even asprintf(), but I can't just see how to mix all these together.

Sorry for the code in Spanish, but I wanted to be sure it's the exact same thing I'm running. Maybe later (if I can) I'll try to translate it, but I'm not sure. Even tough, the important thing is to replace every printf() to something for appending those outputs to mensaje.

+1 this is a simple and straight-forward approach to the problem. If needed you can realloc as you approach a defined thresh-hold from end-of-buffer and reposition buffptr within the realloc'ed base. Nice approach.
–
WhozCraigNov 26 '12 at 1:23

That's corret, @lenik. I mentioned that error handling is a good idea. In practice, I cannot think how sprintf could possibly return an error on these simple strings.
–
paddyNov 26 '12 at 2:06

@paddy yeah, I know - it's kinda what I typed - but the main problem is how to ensure buffer is big enough. I'm writing something like a script interpreter of a minimalistic scripting language (college assignment, yeah), so the pcb->code, for example, can vary between a few bytes to thousands of them. The real problem is what's the best way to ensure buffer is long enough...
–
mgarciaisaiaNov 26 '12 at 5:14

Well, you would keep track of your buffer size and use snprintf to ensure you didn't overrun, then realloc when necessary. Honestly though, you want to start with a buffer size that is going to handle just about anything you might throw at it. You don't want to be reallocating your buffer too often. As for your solution, it's extremely inefficient. Using strlen to calculate the buffer size is just plain wrong. You are constantly reallocating even when you most likely don't need to, because you're not keeping track of the true buffer size.
–
paddyDec 6 '12 at 2:55

I haven't done the math, but I have no way to assure 20k is "large enough" for me. And that's the real problem.
–
mgarciaisaiaNov 26 '12 at 5:17

you may allocate 60k or 100k or several megabytes if necessary, why not?
–
lenikNov 26 '12 at 5:28

because I'm really not sure how big is big enough, and I don't feel like wasting gigs of memory for this, too. Besides, it can't be that hard enough. It's C, I know, but if I'd die trying to concatenate a couple of strings...
–
mgarciaisaiaNov 26 '12 at 5:34

So all you need is a way to append a string to another string. This can be done with a combination of strlen, realloc, strcpy, or memcpy.

The bigger issue is that your dictionary_iterator and pila_hacer functions don't seem to let you pass any additional information to your callbacks. printf doesn't care because it can just use the global stdout, but if you want to have it append to a string, you'll probably need to use a global variable here, so your callbacks know where to append to.

dictionary_iterator and pila_hacer are nothing like a problem - let's assume I can modify them to handle an extra variable. My issue is what's the best way to combine those functions you mention to achieve this. I'd love to have some string_append(dest, source) function that handles all of this dirty memory managment issues.
–
mgarciaisaiaNov 26 '12 at 5:16

I've finally created a string_concat() function that receives a original string, a format and a variable argument list, and applies a vsnprintf with the format and arguments, and appends that to the original string:

EDIT: OK, so my previous approach was buggy. There were problems with the va_list I think. Here's the new version: