6 ms·
Whenever I review C code, I first look at the string function uses. Almost always I'll find a bug. It's usually an off by one error dealing with the terminating
by WalterBright 6y ago
Whenever I review C code, I first look at the string function uses. Almost always I'll find a bug. It's usually an off by one error dealing with the terminating 0. It's also always a tangled bit of code, and slow due to repeatedly running strlen.
But strings in BASIC are so simple. They just work. I decided when designing D that it wouldn't be good unless string handling was as easy as in BASIC.
- mjcohen 6y agoIIRC, in the early days of the Commodore PET, it used a method of keeping track of strings that was fine in an 8k machine but was too slow in a 32k machine. They had to make a change that avoided quadratic time on the larger machine. So string handling in BASIC wasn't always that simple.
- 867-5309 6y ago+1 for the PET mention since it was my first "computer". much overlooked in favour of the 64
- tlavoie 6y agoAh, yes. I recall the luxury of a Commodore of my very own (a C128), after using PETs in school. We had a whole three of them at the time, with a shared, dual-floppy drive for the set. Naturally, our teacher wisely pushed hard on figuring what you could out on paper first.
- dotancohen 6y ago> Naturally, our teacher wisely pushed hard on figuring what > you could out on paper first. Specifically in the case of the Commodores (I grew up on a C128) I find this observation backwards. Sure, if you only had three machines for twenty students then time on the machine was valuable. But on those machines there was so much to explore with poke (and peek to know what to put back). From changing the colours of the display to changing the behaviour of the interpreter. I think that I discovered the for loop at eight years old just to poke faster!
- ajuc 6y agoIt always blows my mind when I remember 8-bit computers had garbage-collected strings.
- anikom15 6y agoI’m not sure if it was the original purpose of C, or of it’s what made C popular, but compared to BASIC, processing strings in C was much faster.
- deleted 6y ago[deleted]
- ajuc 6y agoEverything was faster in C - it was compiled and BASIC was interpreted. Better comparison would be between C and Turbo Pascal strings in DOS times. TP strings were limited to 255 characters but they were almost as fast as C strings, in some operations (like checking length) they were faster, and you had to work very hard to create a memory leak or security problem using them. I've learnt Pascal before C and the whole mess with arrays/strings/pointers was shocking to me.
- indymike 6y agoI learned C before Pascal and having to write so much code to deal with 255 character limits was kind of jarring.
- schlupa 6y agoUCSD and Turbo-Pascal had it easy with the 255 byte strings. They had real strings but these were compiler extensions. Real Pascal didn't have string support and you could only work with packed array of chars of fixed size and as the language was extremely strong types, to packed chars types of different lengths were considered different types, so you had to write procedures and functions for all used packed array sizes.
- mcguire 6y agoBrian Kernighan on "Why Pascal is not my Favorite Programming Language" (https://www.lysator.liu.se/c/bwk-on-pascal.html https://www.lysator.liu.se/c/bwk-on-pascal.html) [1981]. Turbo Pascal wasn't released until 1983, if the wiki is to be believed.
- yholio 6y agoIn the case of C, it's a design decision Denis Ritchie made that came down to the particular instruction set of PDP-11, that could efficiently process zero terminated strings. So a severely memory limited architecture of the 70s led to blending of data with control - which is never a safe idea, see naked SQL. We now perpetuate this madness of nul-terminated strings on architectures that have 4 to 6 orders of magnitude more memory than the original PDP-11. It's also highly inefficient, because a the length of string is a fundamental property that must me recomputed frequently if not cached. Bottom line, unless you work on non-security sensitive embedded systems like microwave ovens or mice, there is absolutely no place for nul-terminated strings in today's computing.
- 10x-dev 6y agoMr. Bright, I just want to thank you for creating D. It is by far my favorite language, because it is filled with elegant solutions to hard language problems. As a perfectionist, there are very few things I would change about it. People rave about Rust these days, but I rave about D in return. Just wanted to say thanks (and that I bought a D hoodie).
- WalterBright 6y agoThanks for the kind words!
- aydwi 6y agoYour words have just convinced me to try out D. Maybe some good will come out of it :)
- dotancohen 6y agoHello 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?