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.
How to find size of an array in C without sizeof
161–170 of 212 posts
Re: How to find size of an array in C without sizeof
#162Earlier 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.
Otherwise I totally agree with your point.
Re: How to find size of an array in C without sizeof
#163Earlier 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.
Re: How to find size of an array in C without sizeof
#164The 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?
Re: How to find size of an array in C without sizeof
#165Earlier 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.
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
#166Earlier 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…
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
#167Earlier 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.
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
#168Earlier 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...
Re: How to find size of an array in C without sizeof
#169Whether 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).
Re: How to find size of an array in C without sizeof
#170Interesting. 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.
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