by pjmlp · 13y ago · 0 comments
- 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?
Thankfully C++ is powerful enough that user types have the same rights as built-in ones.