Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

81–90 of 212 posts

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

#81
post #71
post #60

Earlier quoted context omitted.

Yes &arr does behave like arr when it comes to ptr arithmetic but the compiler does not guarantee that &arr + 1 does not overflow. It only guarantees arr + 1. if you have a ptr from heap, ptr + 1 if not alloced previously is UB. > If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; this p…

How so? If the array has five elements, you can pass &arr to a function that expects an int[5][], and that function certainly can build a pointer that points one past the last element. Likewise, the compiler ensures that you can build &arr[5] and that is the same address as &arr+1. &arr+1 cannot overflow.

The compiler guarantees that arr + 1 doesn't overflow by making sure arr's address is small enough to not overflow when accessing one element past the array size. &arr + 1 is not one past the array you asked the compiler to allocate.

if you're on a 16bit system and you define char x[36], the compiler guarantees that x's address is not more than 65500. if you do &x + 1 then you'll overflow, x + 1 won't.

You can pass whatever you want to the functions and apply the operands you want and the compiler will happily comply with you. But when you pass it 65500 and add 72 to it, it's going to overflow.

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

#82
post #15
post #8

Earlier quoted context omitted.

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.

Where is it dereferencing the array?

    *(&arr + 1) - arr
That translates to taking the address one point past the array and subtracting the address of the array from it. It doesn't actually dereference the location past the end of the array.

While:

    (&arr)[1] - arr
might appear to be doing something different, it actually isn't.

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

#85
post #76
post #18

Earlier quoted context omitted.

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.

>I think this article made a lot of people feel stupid I don't think so. Anyone with a solid understanding of C understands pointer arithmetic. I think the article isn't obvious only to those who have a weak understanding of the language.

There are many that have a weak understanding of the language.

I got asked some years back why I defaulted to C in some interview questions -- I grew up with the language, understand the nuances and many of the implementations.

It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem.

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

#86
post #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.

Nope, you have guarantees about checking the address of one element past the end of an array. Think of all the bugs you'd otherwise enjoy...

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

#87

Earlier quoted context omitted.

Faster to read, and keeps the reader's mind at a semantically higher level.

Provided that he knows about the macro. Otherwise it's slower and if you switch projects often it requires that you remember what's it about. I guess it could be useful for teams working together on bigger codebases.

1. An appropriate named macro shouldn't cause you to need to investigate it unnecessarily

2 most IDEs allow simple hover over and see macro definition without having to break much flow.

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

#88

Earlier quoted context omitted.

this is undefined behavior. &arr + 1 can overflow No. From 6.5.6 Additive operators: 7 For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type. 8 [...] if the expression P points to the last element of an array object, the expression (P)+1 points one past…

So then I guess malloc can't return an allocation which actually goes to the end of the address space, but has to leave at least one extra byte to avoid overflow? That's pretty interesting, though I guess it certainly makes sense. Edit: Also now that I think about it, I've written code that relied on that behavior...not sure if I'd heard it before and internalized and forgot it, or just was being foolish.

Technically, this needn't impact malloc, because dereferencing the "one past the end" address is still undefined. All you need is logic in your pointer arithmetic that essentially treats the past the end address as a special value (which normally would never need to be represented).

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

#89

Whether you use this method of getting the number of elements in an array or the more traditional sizeof method, please encapsulate the logic in a macro. Instead of writing either of these: size_t length = sizeof array / sizeof array[0]; size_t length = (&array)[1] - array; Define this macro instead: #define countof( array ) ( sizeof(array) / sizeof((array)[0]) ) Or if you must: #define countof( array ) ( (&(array))[…

Please don't replace a one line, obviously recognised by every C programmer since the beginning of time, sizeof(array) / sizeof (type) with some macro that not everyone knows. But alas, I've only been a C programmer for 30 years so I probably don't know what is cool these days.

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

#90
post #76
post #18

Earlier quoted context omitted.

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.

>I think this article made a lot of people feel stupid I don't think so. Anyone with a solid understanding of C understands pointer arithmetic. I think the article isn't obvious only to those who have a weak understanding of the language.

>I think the article isn't obvious only to those who have a weak understanding of the language.

Hi! Could you take a guess at what percentage of C programmers who write C professionally fit your definition of that (I realize you were being hasty in your phrasing, but still)?

Obviously your answer should be betweeen 0% (no programmer who writes C professionally) and 100% (every programmer who writes C professionally.)

I'm genuinely curious what you think! Thanks :)

Post reply on HN