Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

61–70 of 212 posts

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

#62

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?"

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

#63

Earlier quoted context omitted.

I doubt the author meant for this trick to be actually used, they were just showing how pointers to arrays are typed correctly in a clever way.

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?

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

#64
post #57
post #45

Earlier quoted context omitted.

Not likely, but possible. This reminds me of the bug that was found in the binary search algorithm a few years ago, IIRC, in Java. The interesting thing is that binary search is probably one of the earliest-invented algorithms. Yet, in the book Writing Efficient Programs by Jon Bentley (which I mentioned in a recent HN comment), he says that in a class he taught to several industrial programmers with many years of ex…

I think this is it: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6412541

Yes, that's it, by the desc. and dates shown.

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

#65
post #30

Earlier quoted context omitted.

I'm not sure if it's a praise for C though. Arcane design and lack of clarity might be fun to decipher, but it's not something that you'd want to see in the programming language.

Doesn't every language turns into insanity to decipher once you look close enough?

I don't think that's universally true, some languages are quite sound and logical at their core.

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

#67

The result you get with this trick is signed, while the result you get with sizeof is unsigned. Edit: Just to clarify, what you get is ptrdiff_t instead of size_t. So if array size is greater than PTRDIFF_MAX, you get undefined behavior [1]. [1] http://en.cppreference.com/w/c/types/ptrdiff_t

How likely do you run into array bigger than 2gb?

Often enough; "pack" files in video games are often many GB. Memory-map one of those and there you are . . .

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

#68
post #44

C is such a boondoggle of a language... We're condemned to forever explore its every weird nook and cranny for historical reasons, rather than because it is the cleanest, best approach to things possible.

C for sure has its weird sides, but does appear much more logical and consistent when observed "from the below", from how-the-hardware-runs perspective. For example, the shift operators have higher precedence than bitwise masking (and/or/xor) since this way the expressions setting/clearing ranges of bits won't require parentheses (so increased readability) and the masking constants in them will be the narrowest. Load…

You could attempt to rationalize some of its (terrible) design decisions after-the-fact by finding convenient examples, but compared to the clarity and surety of straight-up assembly, C is a dystopian nightmare of enormous unseen complexity and undefined behavior.

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

#70
post #67

Earlier quoted context omitted.

How likely do you run into array bigger than 2gb?

Often enough; "pack" files in video games are often many GB. Memory-map one of those and there you are . . .

The parent is taking about >2gb on a 32-bit machine.
Post reply on HN