@b4n, yes, but std::vector is a library type, not a builtin, which keeps track of its size and its capacity (which is why it is possible to insert the assert, its has size to compare to, but in C since nothing saves the size of array you malloced no automatic assert can be inserted in an index operation). Operator[] on a std::vector is not a builtin, its an overloaded operator member function that returns a reference to the indexed member. So your &vector[0] has to call vector.operator[](0) before taking the address of the referenced object when it returns. But of course you can't reference a non-existent object, which is why its UB to do so.

The std::vector has an alternative member function vector.at() that is always checked and which throws an exception if the index is out of range. But of course thats slower, whereas operator[] is as fast as C, so all premature optimisers (basically all programmers :) use it.

As I keep saying C++ is not C 😁


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.