3 ms·
Super 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 a
by synergy20 6y ago
Super 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?
- WalterBright 6y agoMy alternative is to do a strlen for each string, then use memcpy memset memchr instead. > I thought they're a safer strcpy/strcat Let's look at the documentation for strncpy, from the C Standard: "The strncpy function copies not more than n characters (characters that follow a null character are not copied) from the array pointed to by s2 to the array pointed to by s1." There's a subtle gotcha there. It may not result in a 0 terminated string! "If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written." A performance problem if you're using a large buffer. Yeah, always prefer snprintf. The time functions? I'm just very careful using them.