C++, give us the length of our arrays!

By | October 12, 2012

I’ve been the teaching assistant of the C++ course for a while, but I haven’t realized that C++ keeps the lengths of the dynamically allocated objects at the run time until this week. The array deletion operator delete[] which calls the destructor of each array element has to know the number of elements in the array. Ooops! Since I always send an array to a function together with its size, I was thinking that the size information is not stored by the runtime library. That was totally wrong. The techniques for storing the size information can be found here.

Now, I started wondering why there is no length(x) function which returns the length of the array x. Since the delete[] operator is a standard operator, every compiler must know the length of the dynamic arrays at the runtime, and this information can be used to implement length(x).

 

Leave a Reply

Your email address will not be published. Required fields are marked *