Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I can never remember the nuances of the 50 various string functions and which shouldn't be used.

What I do remember is that virtually all string problems can be solved with snprintf/asprintf.



snprintf is a worse strlcpy: not only does it need to call strlen if you pass it a string parameter, it also ends up in ??? territory if the string is long enough because its return type is int.


The printf family of functions also runs a mini-interpreter that has its cost, because its main use is interpolation (the % placeholders). Some compilers can substitute them for more efficient versions (e.g. printf without any % in the format string -> puts). I don't know if they can detect and substitute an snprintf with a "%s%s" format string.


Ah, good old “what if my string exceeds two gigabytes” dilemma.


> and which shouldn't be used.

my rule of thumb is that if its name begins with str then it shouldn't be used.


If you decide to use these functions. Beware of the sharp edges. Read the documentation. Read the documentation for your specific version and platform and compiler you are using. Between different CRT's these things can act differently even though they say they do the same thing.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: