by papsosouid · 13y ago · 0 comments
That is objectively incorrect. There is much less that can go wrong with C, as there is much less period. Everything that you can do incorrectly with C, you can do incorrectly with C++, plus 10 times more things that C++ introduced. The idea that C++ is safer because "we just won't do dangerous stuff" is silly, as any language is safe if you "don't do dangerous stuff", including C.
All languages are safe if you've written perfect code, but no one is perfect, C++ does try to catch some of the lower hanging fruit problems but if you're writing hoary code you're going to blow your foot off eventually.
- use safe arrays with bound checking if you feel like to
- use automatic memory management
- pass arguments by reference and being sure they point to valid data
- use proper strings without caring if the null character is missing
C++ is only dangerous thanks to the C compatibility legacy.
Don't use C'isms and the application will be a lot safer than doing pure C coding.
You can bound-check your arrays at run-time, by wrapping them in a struct and only accessing them with functions.
There's even a proper (although conservative) garbage collector for C, while C++ usually boils down to reference counting.
Even in C you are not restricted to the standard library to handle strings. C doesn't have to mean null-terminated strings. (And in C++, too, string literals give you the old-time bad strings.)
This is no longer a data type seen as a language type, but an Abstract Data Type as known in Computer Science.
You are no longer using arrays, but a data structure made by yourself.
> There's even a proper (although conservative) garbage collector for C, while C++ usually boils down to reference counting.
If you are referering to Boehm-Demers-Weiser GC, it also works in C++.
C++11 also has a GC API.
> Even in C you are not restricted to the standard library to handle strings. C doesn't have to mean null-terminated strings.
The moment you do this, you are the strange kind in town as all libraries expect C style strings as input. So it is conversion party any time you need to call those functions.
> (And in C++, too, string literals give you the old-time bad strings.)
Yeah, this is a consequence of C's compatibility that infected C++.
Yes, it's no longer a built-in data type. But non-builtin-types are perfectly fine, too. By the way, how do you get bounds checking in C++? I guess you use the same technique, but C++'s overloading makes it syntactically easier to hide that you are not using a built-in?
This isn't true. C++ doesn't allow certain implicit casts that C allows (e.g., from void* to T*).