Hacker News new | past | comments | ask | show | jobs | submit login

> There's also the option of using both a length AND a null terminator.

I first encountered that idea in this classic Joel on Software post, which rather put me off the idea of using them in production:

> Notice in this case you’ve got a string that is null terminated (the compiler did that) as well as a Pascal string. I used to call these fucked strings because it’s easier than calling them null terminated pascal strings but this is a rated-G channel so you will have use the longer name.

https://www.joelonsoftware.com/2001/12/11/back-to-basics/




Joel's article is a bit long in the tooth. As of C++11, std::string is required to use both a length and a null terminator. With the short string optimization, it could even have that exact layout.

> Lazy programmers would do this, and have slow programs

    char* str = "*Hello!";
    str[0] = strlen(str) - 1;
Modern compilers understand strlen, and will replace the function call with a constant where possible. That code's not slow anymore: https://godbolt.org/z/Kjh8b44Kf


That's good to know, thanks.

But Joel was talking about C rather than C++, where your comments about std::string wouldn't apply, right?




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

Search: