Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find size of array without using sizeof

Tags:

arrays

c

sizeof

I was searching for a way to find the size of an array in C without using sizeof and I found the following code:

int main ()
{
    int arr[100];
    printf ("%d\n", (&arr)[1] - arr);
    return 0;
}

Can anyone please explain to me how is it working?

like image 480
poorvank Avatar asked Apr 15 '13 15:04

poorvank


People also ask

How do you find the size of an array without using sizeof?

&a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the array. Print the size.

How do I know my size without sizeof?

The idea is to use pointer arithmetic ( (&(var)+1) ) to determine the offset of the variable, and then subtract the original address of the variable, yielding its size. For example, if you have an int16_t i variable located at 0x0002 , you would be subtracting 0x0002 from 0x0006 , thereby obtaining 0x4 or 4 bytes.

How do you find the size of an array?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);


2 Answers

&arr is a pointer to an array of 100 ints.

The [1] means "add the size of the thing that is pointed to", which is an array of 100 ints.

So the difference between (&arr)[1] and arr is 100 ints.

(Note that this trick will only work in places where sizeof would have worked anyway.)

like image 75
RichieHindle Avatar answered Oct 17 '22 12:10

RichieHindle


&arr gives you a pointer to the array. (&arr)[1] is equivalent to *(&arr + 1). &arr + 1 gives you a pointer to the array of 100 ints that follows arr. Dereferencing it with * gives you that array that follows. Since this array is used in an additive expression (-), it decays to the pointer to its first element. The same happens to arr in the expression. So you subtract to pointers, one pointing to the non-existent element right after arr and the other pointing to the first element of arr. This gives you 100.

But it's not working. %d is used for int. Pointer difference returns you ptrdiff_t and not int. You need to use %td for ptrdiff_t. If you lie to printf() about the types of the parameters you're passing to it, you get well-deserved undefined behavior.

EDIT: (&arr)[1] may cause undefined behavior. It's not entirely clear. See the comments below, if interested.

like image 36
Alexey Frunze Avatar answered Oct 17 '22 12:10

Alexey Frunze