Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

71–80 of 212 posts

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

#71
post #60

Earlier quoted context omitted.

That's why I quoted paragraph 7: arr is not an element of an array, so &arr (being a pointer to an object which is not an element of an array) behaves like a pointer to the first element of an array of length one with the type of the arr as its element type. So &arr behaves like it's a pointer to the start of int[5][1].

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.

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

#72

Earlier quoted context omitted.

Indeed, one could hope that is the case! :-) But my point with suggesting the macro applies equally to the more traditional sizeof division. I have seen code that divides the two sizeofs every time an array length is needed. I think it's better to put that calculation in a macro so you only do it in one place.

You are dividing one constant by another -- surely that would be handled at compile time?

You are correct, the compiled code will be the same whether you use a macro or not. In fact, this is true for any C macro. A macro is merely a source code text substitution done by the preprocessor. Using a macro is exactly the same as writing out the equivalent macro expansion everywhere you use it.

My suggestion to use a macro is not because of any difference in the compiled code, but to improve the readability of the source code.

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

#73

Earlier quoted context omitted.

Indeed, one could hope that is the case! :-) But my point with suggesting the macro applies equally to the more traditional sizeof division. I have seen code that divides the two sizeofs every time an array length is needed. I think it's better to put that calculation in a macro so you only do it in one place.

You are dividing one constant by another -- surely that would be handled at compile time?

While it may be optimized, I think the suggestion is that instead of using a hack repeatedly, it is arguably better to be DRY and abstract it away.

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

#74

Earlier quoted context omitted.

> please encapsulate the logic in a macro. Why? When reading such code, it means I would have to go and lookup a macro definition. So, there's a clear drawback. What's the benefit that makes it worthwhile?

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.

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

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

I suspect this article made a lot of people feel stupid

Anyone who doesn't understand pointer arithmetic in C has no business being involved with C. I'm not trying to be negative about this post, but the notion that people are feeling "stupid" about this is hysterical.

HN has absolutely trended toward utterly beginner type C information being some novelty on here. It's a bit bizarre, and the general skill level of the site has catastrophically declined.

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

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

>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.

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

#78
post #38
post #31

Earlier quoted context omitted.

But arr != &arr even though they have the same value. #8 applies to arr (P), but in the post OP is using &arr which is a ptr to array[x] and doesn't apply to it.

Can't we declare pointer of type &arr, assign it there and be sure that it points to equivalent of array[1] of &arr? If yes, then is it logically possible to have UB on that?

You can define a pointer of type `int (*)[5]` and assign `(&arr)[1]` to it. That's fine, it's a pointer to the 5-element array just after the one we're sure is valid.

Dereferencing the pointer is UB, but you can create the pointer, assign it to a variable, etc.

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

#79

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))[…

A more detailed article here: http://www.g-truc.net/post-0708.html with a cleaner way to do _countof using a template in C++ 11. You can also use the template technique to pass a fixed size array to a function, and have the function determine the array size (without needing a 2nd length param, or null terminator element). Similar to strcpy_s(): http://stackoverflow.com/questions/23307268/how-does-strcpy-... MSVC has…

Thanks for the interesting references!

While we're talking macros, anyone who reads the g-truc.net article should feel itchy after seeing the countof macro in their example:

  #define countof(arr) sizeof(arr) / sizeof(arr[0])
Two problems here:

1. The last use of 'arr' doesn't have 'arr' wrapped in parenthesis.

2. The entire expression is not wrapped in parentheses either.

If you write a macro that does any calculation like this, play it safe and put parens around every macro argument and parens around the entire expression too. Otherwise you never know what operator precedence will do to you.

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

#80

Was anyone else's first thought "Hmm... cool," followed by "I hope nobody asks me this on an interview?"

If you are asked this in an interview, it's not longer an interview... I would simply reply "what circumstances would dictate the necessity of such rather than producing clean code for my coworkers?"

Hence why I hope noone asks me it. :)
Post reply on HN