Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

11–20 of 212 posts

Re: How to find size of an array in C without sizeof

#11
post #7
post #2

Given how many bugs & errors stem from simple fails in range checks etc, I would much rather go with the tried and true way rather than use something "clever". Quoting http://stackoverflow.com/a/16019052/1470607 Note that this trick will only work in places where `sizeof` would have worked anyway.

Yes. This only works for arrays on the stack, at best. It assumes that arrays are placed on the stack in the order of declaration, which is not a requirement of the C standard and may differ between compilers. Unless you're writing a buffer overflow exploit, in which case you need to know exactly what's on the stack and where, this isn't a good way to program. Update: misread the article; thought he was differencing…

> It assumes that arrays are placed on the stack in the order of declaration

I am not sure it is the case here. The code uses only one array, how can it assume the order of arrays?

Re: How to find size of an array in C without sizeof

#12
this is undefined behavior. &arr + 1 can overflow. There's no guarantee &arr isn't near memory end boundary. &arr + 1 is converted at compile time to rbp - X where X is an integer determined by the compiler similarly to how sizeof works.

Basically ptr + integer requires the compiler to determine the sizeof ptr's type.

Re: How to find size of an array in C without sizeof

#13
post #7
post #2

Given how many bugs & errors stem from simple fails in range checks etc, I would much rather go with the tried and true way rather than use something "clever". Quoting http://stackoverflow.com/a/16019052/1470607 Note that this trick will only work in places where `sizeof` would have worked anyway.

Yes. This only works for arrays on the stack, at best. It assumes that arrays are placed on the stack in the order of declaration, which is not a requirement of the C standard and may differ between compilers. Unless you're writing a buffer overflow exploit, in which case you need to know exactly what's on the stack and where, this isn't a good way to program. Update: misread the article; thought he was differencing…

I don't see how the code assumes anything about the placement of the array. Indeed, it works just fine for static arrays:

    $ cat test.c
    #include 
    
    int arr[5];
    
    int main(int argc, char *argv[]) {
    	printf("%lu, %ld\n", sizeof(arr) / sizeof(*arr), (&arr)[1] - arr);
    }
    
    $ gcc test.c && ./a.out
    5, 5
Not saying it's "a good way to program" - it's needlessly obfuscated compared to the standard sizeof alternative. But it doesn't rely on anything tricky.

Re: How to find size of an array in C without sizeof

#14
post #3

Despite the argument at the end, this is undefined behavior in the latest C specification. The code dereferences a pointer one past the last element. C11 6.5.6/8: If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated

"it shall not be used as the operand of a unary * operator that is evaluated"

he doesn't use the * operator on it, he just calculates its position. If he were to access it (ie, use it with *) then that would be breaking the rule

Re: How to find size of an array in C without sizeof

#15
post #8
post #3

Despite the argument at the end, this is undefined behavior in the latest C specification. The code dereferences a pointer one past the last element. C11 6.5.6/8: If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated

The snipper only calculates the pointer, and does not dereference it. Should be fine.

It's a complicated situation. There's a pointer to an array, and that pointer is dereferenced, resulting in an array (that then decays to a pointer). But that second array/pointer is not dereferenced. I'm not sure if it's legal.

Re: How to find size of an array in C without sizeof

#18

I'm surprised at all of the comments calling this stupid or pointless. The point is not that you should this trick in lieu of sizeof; the point is to shed light on a subtly of C arrays.

I suspect this article made a lot of people feel stupid, or in other words, it taught us something. Sometimes the ego gets out of check.

I think the article is well-presented and educational.

Re: How to find size of an array in C without sizeof

#19

I'm surprised at all of the comments calling this stupid or pointless. The point is not that you should this trick in lieu of sizeof; the point is to shed light on a subtly of C arrays.

Quite. This exactly the sort of thing that makes C such a fun language.

Re: How to find size of an array in C without sizeof

#20
For the completeness sake, the size of an array can also be computed via linker symbols, see for example: http://stackoverflow.com/questions/29901788/finding-the-last....

Same constraints apply (pointer arith).

I am not sure why this method, applied to ordinary arrays, would be preferred to sizeof (), but since we're shedding light here...

EDIT: pointer arith constraints only apply if we compute the difference (end - beg) in the C code. We could also do that in the linker script itself, and I don't recall whether or not C semantics of ptrdiff_t would be preserved in that case. Such preservation doesn't seem very probable to me, so potentially this method might allow to avoid overflows (or to move them much higher) -- to be checked in the 'ld' doc!

Post reply on HN