4 ms·
True that there is a safe way of writing C, if I followed x guidelines, and did Y checks. But, I cannot in good conscience trust all the libraries and tools I i
by reacharavindh 8y ago
True that there is a safe way of writing C, if I followed x guidelines, and did Y checks. But, I cannot in good conscience trust all the libraries and tools I import have gone through the same diligence.
Another thing is, even the basic constructs of the language (for simpler needs like the ones I described in original question)are deemed unsafe - like string processing. Someone told me in passing that I should use Redis's SDS[1] if I want safe strings in C. Do not use char * and manipulate them. I admit that I haven't spent my time understanding why that is the case, or have validated whether it is mitigated magically by using SDS. The point is the need for doing such things for basic elements of a language - strings.
What I wonder is why there isn't a language that is C, with safe defaults, easy-to use syntax, as fast as C, and consequentially less powerful. I was hoping Rust was it, until I started to feel scared just looking at the code. At the risk of prejudice, I feel it is more of a glorious C++ than a language I could use for simple general purpose programs(that are still as fast as C).
[1] - https://github.com/antirez/sds https://github.com/antirez/sds
EDIT : Grammar and reference
- bigato 8y ago> What I wonder is why there isn't a language that is C, with safe defaults, easy-to use syntax, as fast as C Because part of the speed of C is due to the fact that it doesn't do much for you. The moment you want the language to guarantee your memory safety, you'll have to pay either via speed penalty or via added complexity. At least this has been the case with the languages I know. Maybe someday something else will emerge which proves me wrong.
- gnode 8y ago> I was hoping Rust was it, until I started to feel scared I recommend you stick with it. Your understanding of it will improve with practise. Memory safety isn't free -- there's either a language complexity (borrow checking), a runtime complexity (garbage), or a programming complexity (avoiding making mistakes). While there's no one correct option, there is in my opinion a wrong one. Human minds are fallible.
- gravypod 8y agoFunnily enough I was considering including sds as an example in my intial post. The misconception that people have is that C has data structures provided. C is a language of only primatives. There are pointers and there are different width numeric types and structures comprised of those numeric/pointer types. Nothing else is provided by default. No strings, no lists, nothing. For many situations operating at this level of abstraction is ok. Many programs do not ever need a string type. Unfortunately in situations where strings were needed initially the people at Bell Labs decided to use a pointer to a character and come up with convention for that layout of memory. This means that everything must understand those conventions! This is a dangerous and leaky abstraction. As the more times you implement something the more times you can mess it up. SDS provides a single implementatuon for string manipulation by hiding data within the pointer of the string and type aliasing the original char*. It is abstracted from your initial stucture. If you do this with everything you write, using the type system to your advantage, than most of your issues with C will not be an issue. I know the guy behind redis has many videos on his software. From what I've heard he is a modern day software engineering genius and I'm assuming he will have good examples of how to implement this non-leaky abstraction in his software walkthroughs.
- ht85 8y agoRust forces you to formalize memory ownership, and for complicated usages it can become pretty scary, yes. Have you tried to figure how the safe subset of C you're suggesting would look in Rust? If you're doing something simple, it shouldn't be that bad. Do you have a specific use-case in mind? I'm curious what your requirements are in terms of performance and simplicity, that languages like Go, Java or even Haskell do not meet. Is it a platform thing?