Well I don’t think that it’s really important but since the program has to store the length because of delete[] anyway, Why can’t we get this “stored information” ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The implementation only needs to store the length, and typically only does, if the type is not trivially destructible (i.e., it needs to generate calls to a destructor) and the array was created with the new[] operator.
Since that property of the arrayed type bears no relation to the size of the array, it is more elegant simply to call the length “cookie” a private implementation detail.
To get the length of a complete array object (not a mere pointer), you can use
std::extent< decltype( arr ) >::valueorstd::end( arr ) - std::begin( arr ).Using
new[]with a class with a destructor is a code smell. Considerstd::vectorinstead. The overhead vs rawnew[](considering all bytes that need to be allocated, wherever they are) is one pointer’s worth of bytes, and the benefits are innumerable.