[OCLUG-devel] sizeof an array of pointers

Dan Stromberg strombrg at dcs.nac.uci.edu
Mon Jun 20 10:50:23 PDT 2005


On Sun, 2005-06-19 at 22:16 -0700, Christopher Smith wrote:
> Doug Jolley wrote:
> 
> >>Normally libraries like this address
> >>the issue either by terminating the array with a null pointer or have a
> >>second argument somewhere that specifies the length of the array. I'd
> >>check the docs on the library.
> >>    
> >>
> >
> >The library does terminate the array with a null pointer.  So, are you
> >saying that the way to go is to just loop through the array and count
> >the elements?  I don't have any problem with that.  I just thought
> >that if there were a better way, I'd rather use it.
> >
> >Thanks, Chris, for the input.
> >  
> >
> Yeah, if you need to find out the size of the array, that'd be the way 
> to do it. Normally you just want to perform some operations over the 
> elements of the array so it's less of a hassle. You can save yourself a 
> *tiny* bit of overhead for large arrays by using pointer arithmetic. 
> Something like this:
> 
> char** the_array = library_function(...);
> char** end_array = the_array;
> while (end_array != 0) {
>     ++end_array;
> }
> /* 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.

Actually, even pretty primitive optimizers, by modern standards, should
be able to rewrite array arithmetic to pointer arithmetic behind the
scenes, which means you still get good code generated by the backend,
without confusing the pascal programmers :)

Also, ISTR that when you subtract a pointer from another pointer of the
same type, the result is not the size of an element times the number of
elements, but just the number of elements.  For example:

seki-strombrg> make clean; make
rm -f *.o core array-arith
gcc -g -ansi -Wall -pedantic array-arith.c -o array-arith
./array-arith
size of entire array is 2000
Number of elements in the array is 201
Mon Jun 20 10:49:16

seki-strombrg> cat array-arith.c
#include <stdio.h>

#define N 200
struct foo {
        char a[10];
} foo[N];

int main()
{
        printf("size of entire array is %d\n",sizeof(foo));
        printf("Number of elements in the array is %d\n", (&foo[N] - &foo[0])+1);
        return 0;
}

Mon Jun 20 10:49:20

...which I guess just backs up the idea that pointer arithmetic is
overcomplicated.  :)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://localhost.localdomain/pipermail/oclug-devel/attachments/20050620/44a86653/attachment.bin 


More information about the OCLUG-devel mailing list