Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this way of computing array-length work? [duplicate]

i am new to c++ and stumbled upon this way of computing the length of an array with pointers which i don't exactly understand. I looked everywhere but nowhere seems to be an explanation on how it works, i just heard that it's supposed to be a bad way of computing array length but why is that and how does it even work?

The code would look something like this:

int array[4] = [0, 1, 2, 3]
//...
int length = *(&array + 1) - array

As far as i've tried, it really seems to work, but i don't exactly understand why. I know a bit of pointer logic but this statement seems really odd to me, because you're essentially taking the address of the array (the first element i suppose) and adding one to it (i can imagine that that will give you the address after the last element, but then i don't understand why you would dereference it). And what confuses me most, is that this all gets substracted with the array itself?! Without an index or anything.

It would really help when someone would be able to explain that to me, and why it's supposed to be bad exactly.

Thanks.

like image 902
Aerotrix Avatar asked Dec 11 '25 21:12

Aerotrix


1 Answers

&array

This is a pointer to the object array. It is a singular object of an array type.

&array + 1

Adding a number to a pointer produces a pointer to a successive sibling of the object in an array of objects. Adding 1 produces the next sibling. For purposes of this pointer arithmetic, singular objects are treated as array of single object. Hence, adding 1 is allowed and it produces a pointer past the end of the figurative array.

*(&array + 1)

Strictly speaking, this indirects through a pointer past the end, and it may be argued that the behaviour of the program is undefined.

But let's assume that's not a problem. The indirection operation produces an lvalue to the (non-existent) object at the address after the array.

*(&array + 1) - array

Here, the operands of the subtraction are lvalues to arrays. One is the actual array and the other is a hypothetical sibling element in a hypothetical array of arrays. In this case, those arrays implicitly convert to a pointer to first element of the respective arrays.

Technically, the subtraction between the converted pointers is undefined because they are pointers to elements of separate arrays, so arguably the behaviour of the program is undefined for yet another reason.

But let's assume that's not a problem. The result of subtracting pointers to two elements of the same array produces the distance of the elements. The distance between first elements of adjacent arrays is exactly the number of elements in the first array.

why it's supposed to be bad exactly.

Notice the parts in previous sections that say behaviour of the program is undefined. That's bad.

Also, you had a problem understanding what it does. That's bad.

Recommended way to get the size of an array is to use std::size(array).

like image 113
eerorika Avatar answered Dec 13 '25 12:12

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!