Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

161–170 of 212 posts

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

#161

Earlier quoted context omitted.

If you're taking CS classes and boasting about how you don't know how to program, it is a problem.

In many more theoretical CS classes, programming is not a requirement.

There's a word for that. It's "math".

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

#162

Earlier quoted context omitted.

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.

I might go a step further and append "an appropriately implemented macro". Just because something has a good name doesn't mean it's not filled with crazy.

Otherwise I totally agree with your point.

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

#163

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 would hope a practicing programmer would realize that sizeof is a keyword and evaluated at compile time, and use that. I don't consider this article to be an example of something that you should consider putting in your codebase; but an investigation into some of the language's rules.

well, the title ("how to find size of an array in C without sizeof") certainly made it sound as though there was some use to this!

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

#164

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?

It's quite easy to serve over 2 GB of spaces over the network. (gzip, brotli)

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

#165
post #96

Earlier quoted context omitted.

That's why it's called one in a million...

One in a million happens quite often if you're processing something like ~100k requests a second.

In fact I agreed with the parent and just posted a tongue in cheek remark.

One in a million literally means that at ~100k requests a second it will happen once every 10 seconds.

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

#166
post #152

Earlier quoted context omitted.

Is there a circle in hell reserved for C standards committee members who add to the number of cases where 'undefined behavior' occurs in the standards?

Read "undefined behavior" as "depending on architecture and compiler". It's not like anything can happen, but it's simply not to describe every architecture and every compiler into a standard. Sure, somebody is free to write an implementation where a nuke is launched every time "undefined behavior" is encountered, and they would be right according to C standard, but in real world, you pretty much know what to expect…

Not at all - that would be implementation defined behavior.

These days, compilers quite often speculate on undefined behavior, generating code as if the undefined part cannot happen - the result is that your code is going to do stuff you pretty much can not know or expect.

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

#167
post #112
post #82

Earlier quoted context omitted.

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.

&arr is a pointer to an array (it points to the existing array). &arr + 1 is a pointer to an array that begins just after the existing array. * is the dereference operator, so it seems to me that *(&arr + 1) dereferences the pointer to the array, resulting in an array (or a reference to an array), which then decays to a pointer.

>so it seems to me that (&arr + 1) dereferences the pointer to the array

It doesn't. Because an array is already a pointer, in (&arr + 1) &arr is a pointer to a pointer (ie, a handle) so *(&arr) is dereferencing the handle to the pointer. So it's still one pointer level deep - it doesn't dereference it completely.

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

#168
post #54
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…

https://research.googleblog.com/2006/06/extra-extra-read-all...

Yes, thanks, that closely describes the assignment I read that he gave.

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

#169
post #33

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

IIRC it is canonically called NELEMS(a).

I don't know if there's such a thing as "canonical" here. In MSVC, it's _countof (and it's in one of the standard headers).

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

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

The reason why people don't usually run into this is because C tries really hard to decay your arrays to pointers to first element, so there are very few cases where it actually comes up - sizeof(array) and &array are some of the few. On top of that, writing down the type of such an array is not exactly obvious, and requires parentheses:

    int (*p)[10];
This all is much more interesting in C++, because there, in conjunction with references, this lets you write functions that take arrays as arguments and know their length. Like so:

    template
    void foo(const int (&a)[N]) {
        for (size_t i = 0; i 
Post reply on HN