[OCLUG-devel] sizeof an array of pointers

Christopher Smith x at xman.org
Mon Jun 20 00:31:29 PDT 2005


Christopher Smith wrote:

> /* now we know end_array is pointing just after the last element of 
> the array */
> size_t number_of_array_elements = (end_array - the_array) / 
> sizeof(char*);
>
> So, that saves you one increment operation inside the loop. Depending 
> on the speed of integer divide on your platform (these days it's 
> effectively as fast as integer add), this becomes a net win as the 
> array gets bigger.

Oh... how embarassing. I shouldn't write e-mails while trying to get a 
baby to go to sleep. :-(

The divide is unecessary. Thanks to the wonder that is pointer 
arithmetic this is the correct way to do things:

size_t number_of_array_elements = end_array - the_array;

So, that should make this as efficient or more efficient than using a 
counter in almost all cases (zero-length arrays being the exception).

--Chris



More information about the OCLUG-devel mailing list