views:

408

answers:

5

For doing string concatenation I've been doing basic strcpy,strncpy of char* buffers. Then I learned about the snprintf and friends.

Should I stick with my strcpy,strcpy + \0 terminiation. Or should I just use snprintf in the future?

thanks

+4  A: 

snprintf is more robust if you want to format your string. If you only want to concatenate, use strncpy (don't use strcpy) since it's more efficient.

Yassin
+1  A: 

All *printf functions check formatting and expand its corresponding argument, thus it is slower than a simple strcpy/strncpy, which only copy a given number of bytes from linear memory.

My rule of thumb is:

  • Use snprintf whenever formatting is needed.
  • Stick to strncpy/memcpy when only need to copy a block of linear memory.
  • You can use strcpy whenever you know exatcly the size of buffers you're copying. Don't use that if you don't have full control over the buffers size.
jweyrich
A: 

strcpy , strncpy etc only copies strings from one memory location to another. But, with snprint, you can do more stuff like formatting the string. Copying integers into buffer etc.

It purely depends on your requirement which one to use. If as per your logic, strcpy & strncpy is already working for you, no need to jump to snprintf.

Also, remember to use strncpy for better safety as suggested by others.

Jay
I know what you mean, but `strncpy` isn't restricted to reading (or even writing) NUL-terminated strings.
jamesdlin
Yes, I know. Probably, I didn't phrase my answer correctly. I have edited it. Thanks for pointing it out.
Jay
+3  A: 

For most purposes I doubt the difference between using strncpy and snprintf is measurable.

If there's any formatting involved I tend to stick to only snprintf rather than mixing in strncpy as well.

I find this helps code clarity, and means you can use the following idiom to keep track of where you are in the buffer (thus avoiding creating a Shlemiel the Painter algorithm):

char sBuffer[iBufferSize];
char* pCursor = sBuffer;

pCursor += snprintf(pCursor, sizeof(sBuffer) - (pCursor - sBuffer),  "some stuff\n");

for(int i = 0; i < 10; i++)
{
   pCursor += snprintf(pCursor, sizeof(sBuffer) - (pCursor - sBuffer),  " iter %d\n", i);
}

pCursor += snprintf(pCursor, sizeof(sBuffer) - (pCursor - sBuffer),  "into a string\n");
therefromhere
It'd be even clearer if you used a single call to `snprintf`. And be aware that if the buffer isn't large enough, `snprintf` will return the necessary buffer size (or a negative number on failure), so incrementing `pCursor` like that can be dangerous.
jamesdlin
Do a s/snprint/snprintf/ in your code. Also, your example doesn't make sense. I'd stick to formatters.
jweyrich
@jweyrich - whoops, thanks fixed function name. Yeah, it's a dumb example, made it a bit more useful.
therefromhere
@jamesdlin - yeah, I know, either needs to have known bounds on the string size or some error checking.
therefromhere
A: 

sprintf has an extremely useful return value that allows for efficient appending.

Here's the idiom:

char buffer[HUGE] = {0}; 
char *end_of_string = &buffer[0];
end_of_string += sprintf( /* whatever */ );
end_of_string += sprintf( /* whatever */ );
end_of_string += sprintf( /* whatever */ );

You get the idea. This works because sprintf returns the number of characters it wrote to the buffer, so advancing your buffer by that many positions will leave you pointing to the '\0' at the end of what's been written so far. So when you hand the updated position to the next sprintf, it can start writing new characters right there.

Constrast with strcpy, whose return value is required to be useless. It hands you back the same argument you passed it. So appending with strcpy implies traversing the entire first string looking for the end of it. And then appending again with another strcpy call implies traversing the entire first string, followed by the 2nd string that now lives after it, looking for the '\0'. A third strcpy will re-traverse the strings that have already been written yet again. And so forth.

So for many small appends to a very large buffer, strcpy approches (O^n) where n is the number of appends. Which is terrible.

Plus, as others mentioned, they do different things. sprintf can be used to format numbers, pointer values, etc, into your buffer.

janks
Woops, I just noticed you asked specifically about snprintf. The idiom can still work, but you need to also keep a running count of how few characters remain in the buffer. It then becomes `written = snprintf(buf, remanining, ...); buf += written; remaining -= written; `
janks
Extra care is needed with `snprintf`. See my comment to therefromhere's answer.
jamesdlin