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

You seem to confusing C and C++, there is no std::string in C and MRI is written in C.

A 'C String' is by quasi definition, a segment of memory that can be properly processed by the string functions in the C standard library, which requires null termination.

>Now the reason why they technically are (i.e. the reason NULLs are stored at the end of the string) is for compatibility and optimization.

Really its just because they are C Strings, that is they use the C standard library string functions, if you want to use them, you must null terminate.

>Therefore, while null-termination is absolutely NOT required when dealing with an exclusively counted-length implementation of C strings (a la RString, CString, std::string, etc.)

None of those are implementations of "C Strings", they aren't even available for C.

The determination as to whether your using null terminated strings or not comes down to the String library your using. If your on C, your probably using C std lib and need to null terminate your 'strings'. There really isn't much more to it than that.



No, I'm perfectly well-versed in the differences between C and C++, having written in one or the other for a long time. A trivial look-alike implementation of std::string can be written in C, and would look a lot like the RString class.

Your argument is actually, essentially mine. The need to use the platforms' string functions heavily swings (but does not force) the choice of null-terminating the RString members. As I mentioned, it would be really stupid but entirely possible to simply clone the non-null-terminated string into a temporary null-terminated char array every time you want to use a function that takes standard "C strings" if you really, truly, madly wanted to have an RString implementation that was one byte smaller to store. But that would be insane.




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

Search: