4 ms·
What do people get out of pedantic rants such as this? Anyone who's written anything in C is quite aware of the issue here. And then there's this gem: "...If
by JustSomeNobody 11y ago
What do people get out of pedantic rants such as this? Anyone who's written anything in C is quite aware of the issue here.
And then there's this gem:
"...If the source string is 5 characters long, and the target is a 1024-byte buffer, and you set n to the size of the target, strncpy will copy those 5 characters and then fill all 1019 remaining bytes in the target with null characters. Since all it takes to terminate a string is a single null character, this is almost always a waste of time.
Ok, so that's not so bad. CPUs are fast these days, and filling a buffer with zeros is not an expensive operation, right? Unless you're doing it a few billion times, but let's not worry about premature optimization."
Does he think languages with built in String types have some magical optimization juice that make string operations fast? How long does it take to instantiate a new String object, run it's copy constructor, blah blah.
Any C programmer looks to avoid these types of situations.
- _kst_ 11y ago> What do people get out of pedantic rants such as this? Anyone who's written anything in C is quite aware of the issue here. Unfortunately, that's not the case. I presume this link was posted here as a result of my recent comment on the "Don't Learn C the Wrong Way" article, which recommends strncpy() and strlcpy() as safer alternatives to strcpy(). I've see a number of calls to strncpy(). I've rarely seen such calls explicitly null-terminate the destination array. I've even seen things like strncpy(dest, src, strlen(src)); which is certainly no safer than strcpy(). What I get out of this particular pedantic rant is an opportunity to let C programmers know that strncpy() isn't what they might thing it is (as well as a mention on the front page of Hacker News!).
- _kst_ 11y ago> Does he think languages with built in String types have some magical optimization juice that make string operations fast? How long does it take to instantiate a new String object, run it's copy constructor, blah blah. No, I don't think that. I didn't mention it because I wasn't talking about languages with built-in String types; I was talking about C.