Comments by "MrAbrazildo" (@MrAbrazildo) on "Cs Biggest Mistake | Prime Reacts" video.
-
7:14, the only "unsafe design" about those is that, when the vector changes its size, it's not granted to stay at the same location in memory, so the iterators keep pointing to old address. 1 just needs to "refresh" them. But this need exists only 1 time per allocation (size changing). This is not made automatically due to possible performance. It's like web pages that are always refreshing themselves vs those waiting for the user to do it: the 1st is more comfortable, but wastes performance/resources from the machine.
The operator [] hasn't this issue, because it comes all the way from the beginning. But has a performance penalty. I personally use iterators intensively. I only had this issue once.
8:55, agree. This is awkward because, for every 1 of the millions of f()s, the code will has this amount of lines. The way I use to do this is to write a macro only 1 time, calling it everywhere:
#if NEWC
#define arrpass(type, name, dim) type name[..]
#elif C99
#define arrpass(type, name, dim) type name[dim]
#else
#define arrpass(type, name, dim) type *name
#endif
Then f()s will be written like (doesn't matter the standard):
extern void foo (const size_t dim, arrpass (char, a, dim));
1
-
2:53, this is so easily solved by OO...
5:50, you should has went to C++ instead. You would get a shorter/cleaner syntax and faster language, compared to awful Java.
For C, this can be solved by just creating a struct that carries its length:
struct ArrayWithLength {
int thearray[ARRAY_SIZE];
enum { size = ARRAY_SIZE };
};
But the company still has to write alternative f()s for all standard library, to check array size automatically, at each call. I recommend even to write an app to forbid the programmer to call unsafe libraries directly, by statically checking the code.
All of this is solved at once by just switching to C++. Its std::array has begin/end f()s, giving iterators for its limits, keeping the same syntax of any other container, throughout its entire standard library.
6:45, right at its 1st standard, C++ had a fully safe, modern, easy-to-use string class. It hasn't the \0 terminator problem, it keeps the size internally, it's compatible with all C libs and, for the user, since C string literals have implicit \0 in it, with std::string 1 can forget the terminator, even when expanding the string.
1