C does not have a strong type system.
C ++ is a little stronger typed, but in fact it is not a real strong type system, since it has reinterpret_cast .
For a type system that prevents buffer overflows, type information must either (1) indicate an arbitrarily long string, or (2) encode the length of the buffer in the type itself. In addition, the type estimation system must ensure that the buffer length is less than or equal to the conversions.
Edit:
With some caution and neglecting the capabilities of cast-tastic C ++, you can write a “reasonably strong” buffer class without overflow in C ++. However, this is not strictly typified for a general definition of the term, since you can try to access the buffer at an invalid point and still compile it. Someone is much better at templates than I could write a really customized SafeBuffer template.
Here is my cut:
template<int Length> class SafeBuffer { unsigned char[Length]; public: unsigned char& operator[](int index);
Note that we use a template type evaluation system to cause a compilation error buf = bar . This is an example of what a strongly typed system can do (also note that casts can 100% destroy typing in C ++).
source share