Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

131–140 of 212 posts

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

#131

Earlier quoted context omitted.

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

The "how likely is it, really?" response to questions of technical correctness has always bothered me. It takes a mindset completely alien to mine to say "Here's a race condition. Sure, it's undefined behavior, but the race is narrow, so it's rare" or to say "Sure, memory allocation can theoretically fail, but in practice almost never does" or to say "fsync is too slow and most computers have batteries these days". S…

>he "how likely is it, really?" response to questions of technical correctness has always bothered me.

But the question is important in another context: language design. Why is this undefined behavior something that exists in the first place? Objects larger than PTRDIFF_MAX could just not be allowed! This avoids the problem and makes code easier to reason about, with pretty much no downside.

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

#132

Interesting. I've been working with C for almost 30 years (first taught it to myself when I was 14) and never thought about the actual type of array .

You're not alone. I've been programming in either C or C++ for 25 years, and it wouldn't have occurred to me that you can have a "pointer to array of size N" that includes the size. Though I probably could have been led there with a little Socratic questioning.

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

#133
post #94
post #65

Earlier quoted context omitted.

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

Name a language and someone will point out WTFs and subtle issues you might have never considered. The problem is the same when designing a language: computers think best with a large set of simple rules, humans think best with a small set of complicated rules, each having exceptions and different tiers of complexity for different levels of each developer's understanding. I find C somewhat logical, but it has an easy…

>> humans think best with a small set of complicated rules

? I'd reword that to say humans are more entertained by a small set of complicated rules. Simple rules are easy for humans, but they can be boring.

However, in reality here, we're comparing apples to oranges. The instruction set for a computer is it's language. Asking a computer to speak English - that's our (humans') language, and with the tables turned, one could ask whether the computer thought better in English or Chinese, and the answer may be different but still meaningless.

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

#134
post #129

Earlier quoted context omitted.

so you think 85% of programmers who write C can parse (&arr + 1) - arr to find the size of the array, without the use of the article? This is surprisingly high and I am pretty sure at least the majority of people who get paid to write C would fail that. Not because it's not the case that they "should" know it, but simply because it's possible to write C without knowing it, and some people do so. For example consider…

I can only speak for myself. I wrote C code for many years, a long time ago. I'm 100% certain that I never had occasion to use a "pointer to array" type. If you asked me a series of leading questions, like "Can you have a pointer to array of 10 ints?" and "What would happen if you increment that?" I would probably get the right answer, with low confidence. There's almost no way I would have thought of this way of get…

yes; my thinking is that most people writing it today would be in the same category.

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

#135
post #130

Earlier quoted context omitted.

Understanding pointers and pointer arithmetic is fundamental to understanding c. Most books and courses would spend a considerable amount of time and effort to make sure the student understands that. So 'arcane' is the wrong word I think. You just need to get it, and really its no harder than, say context managers in python, or promises in js. Its not relevant at what 'level' those constructs are. They are novel in t…

I'm squinting very suspiciously at these comments suggesting this is about "pointer arithmetic". This is really about the little-used fact that you can have a "pointer to array of size N" type.

Think more about two dimensional arrays.

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

#136

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

If the macro is named appropriately, you don't have to go look up anything. And even if you do, you do it once (per project perhaps). No big deal.

I mean, you don't go look up the definitions of every function that gets called, every time they are called, right?

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

#137
post #132

Interesting. I've been working with C for almost 30 years (first taught it to myself when I was 14) and never thought about the actual type of array .

You're not alone. I've been programming in either C or C++ for 25 years, and it wouldn't have occurred to me that you can have a "pointer to array of size N" that includes the size. Though I probably could have been led there with a little Socratic questioning.

If you start thinking about two dimensional arrays, you'll probably get close quickly.

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

#138
post #84

Interesting. I've been working with C for almost 30 years (first taught it to myself when I was 14) and never thought about the actual type of array .

Which kind of explains so much about the problem with C. ;-)

I think it more explains that you can do a lot without fully understanding what it is you are working with.

Which can be good or bad.

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

#139
post #4

How is this better than the sizeof method? This looks like a clever way to access sizeof information without explicitly using the sizeof operator.

It isn't better. I don't think it was claimed to be.

But if you really understand C, it should also not be a surprise that it works this way.

Post reply on HN