Remix.run Logo
tom_ 3 days ago

Another tip: don't use normal asprintf as-is, but write your own very similar helper!

1. have it free the passed-in buffer, so that you can reuse the same pointer

2. have it do step 1 after the formatting, so the old value can be a format argument

3. when getting the size of the full expansion, don't format to NULL, but do it to a temp buffer (a few KB in size) - then if the expansion is small enough, you can skip the second format into the actual buffer. Just malloc and memcpy. You know how many chars to memcpy, because that's the return value from snprintf

(Don't forget to check for errors and all that.)