5 ms·
Hello Walter! All things considered, you are probably the best person to ask for tips on string handling in C. Would you might sharing the things that you look
by dotancohen 6y ago
Hello Walter! All things considered, you are probably the best person to ask for tips on string handling in C.
Would you might sharing the things that you look for, from the obvious to the subtle? I would love to see some rejected push requests if possible. If I were writing C under your direction, what would you drill into me?
Thank you, it is an honour to address you here.
- WalterBright 6y ago1. whenever you see strncpy(), there's a bug in the code. Nobody remembers if the `n` includes the terminating 0 or nor. I implemented it, and I never remember. I always have to look it up. Don't trust your memory on it. Same goes for all the `n` string functions. 2. be aware of all the C string functions that do strlen. Only do strlen once. Then use memcmp, memcpy, memchr. 3. assign strlen result to a const variable. 4. for performance, use a temporary array on the stack rather than malloc. Have it fail over to malloc if it isn't long enough. You'd be amazed how this speeds things up. Use a shorter array length for debug builds, so your tests are sure to trip the fail over. 5. remove all hard-coded string length maximums 6. make sure size_t is used for all string lengths 7. disassemble the string handling code you're proud of after it compiles. You'll learn a lot about how to write better string code that way 8. I've found subtle errors in online documentation of the string functions. Never use them. Use the C Standard. Especially for the `n` string functions. 9. If you're doing 32 bit code and dealing with user input, be wary of length overflows. 10. check again to ensure your created string is 0 terminated 11. check again to ensure adding the terminating 0 does not overflow the buffer 12. don't forget to check for a NULL pointer 13. ensure all variables are initialized before using them 14. minimize the lifetime of each variable 15. do not recycle variables - give each temporary its own name. Try to make these temporaries const, refactor if that'll enable it to be const. 16. watch out for `char` being either signed or unsigned 17. I structure loops so the condition is <. Avoid using <=, as odds are high that'll will result in a fencepost error That's all off the top of my head. Hope it's useful for you!
- sn41 6y agoThank you for the great list. Could you give examples of 8. subtle errors in online documentation?
- WalterBright 6y agoThe trouble stems from the C Standard being copyrighted. Hence, anyone writing online documentation is forced to rewrite and rephrase what the Standard says. The Standard is written in very precise language, and is vetted by the best and most persnickety C programmers. But the various rewrites and rephrases? Nope. If you absolutely, positively want to get it right, refer to the C Standard. printf is particularly troublesome. The interactions between argument types and the various formatting flags is not at all simple. Other sources of error are the 0 handling of `n` functions, and behavior when a NaN is seen.
- dotancohen 6y agoThank you Walter! I will be sure to internalize this. There are some terrific tips in here, such as using shorter array lengths for debug build and avoiding <= as a loop condition. And I don't recall ever seeing char signed, but now I'm terrified. Thank you, have a great weekend!
- WalterBright 6y agochar being signed used to be commonplace. But it is allowed by the C Standard, and it's best not to assume one way or the other.
- synergy20 6y agoSuper insightful list. What will be the alternative for strncpy/strncat? I thought they're a safer strcpy/strcat but now I need something to replace them. I assume snprintf for sprintf, vsnprintf for vsprintf. No idea what to do with gmtime/localtime/ctime/ctime_r/asctime/asctime_r, any alternatives for them too?
- 6y ago