[OCLUG-devel] sizeof an array of pointers

Christopher Smith x at xman.org
Sun Jun 19 19:27:53 PDT 2005


Doug Jolley wrote:

>I'm using a library routine that defines array as 'char **array'.  I
>thought I  would be able to ascertain the size of the array (in bytes)
>by using 'sizeof(array)'.  What I'm getting is '4' which is not the
>size of the array in bytes.  It's not the number of elements in the
>array either.  I think it MIGHT be the number of bytes in the pointer.
> So, my question is:  What do I need to do to get the size of the
>array in bytes?  My ultimate objective is to get the number of
>elements in the array; but, I think that should be fairly easy after I
>get the array size in bytes.
>  
>
You are right that it's returning the size of a pointer to char*. There 
is essentially no way to get the size of said array without some really 
hairy runtime specific hacking. The compiler can't possibly know the 
size at compile time, the runtime might know it depending on how memory 
allocation is done, but C doesn't provide a standard (or even 
semi-nonstandard) way to get at it. 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.

--Chris



More information about the OCLUG-devel mailing list