News for hackers. Stuff that matters.
Re:How to find size of an array in C without sizeof by ycmbntrthrwaway on Monday December 26, 2016 @03:25PM (#13259608)
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

Re:How to find size of an array in C without sizeof by tedunangst on Monday December 26, 2016 @04:54PM (#13259976)
As far as I know, every compiler is badly broken with arrays greater than SIZE_MAX / 2, so this would be the least of your troubles.
Re:How to find size of an array in C without sizeof by pascal_cuoq on Tuesday December 27, 2016 @05:37AM (#13262405)
Since I have it at hand, here is a list of examples of how compilers are broken if an array is larger than SIZE_MAX / 2 (which is called PTRDIFF_MAX in the post):

http://trust-in-soft.com/objects-larger-than-ptrdiff_max-byt...

Re:How to find size of an array in C without sizeof by mannykannot on Monday December 26, 2016 @08:00PM (#13260637)
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?
Re:How to find size of an array in C without sizeof by microtherion on Tuesday December 27, 2016 @01:53AM (#13261693)
Just how would you make this case defined? The alternatives to undefined behavior tend to be (1) being silent about the issue and letting users and implementers find out themselves (2) defining the behavior in a way that makes it difficult to implement on some architectures or (3) defining the behavior in a way that imposes costs on all architectures. None of these is particularly attractive.
Re:How to find size of an array in C without sizeof by mannykannot on Tuesday December 27, 2016 @07:08AM (#13262664)
I would go with option 1 because it does not suggest the problem is solved. As I write this, I realize that there is a counter-argument in that introducing ptrdiff_t gives you a place to issue a warning.
Re:How to find size of an array in C without sizeof by kbart on Tuesday December 27, 2016 @02:00AM (#13261710)
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 on a given system.
Re:How to find size of an array in C without sizeof by DougBTX on Tuesday December 27, 2016 @02:22AM (#13261773)
> "depending on architecture and compiler".

The standard also has the idea of "implementation defined" behaviour, which is close to the definition above. "Undefined behaviour" is a trickier beast, since compilers can rightly assume undefined behaviour never occurs, and optimise accordingly.

Re:How to find size of an array in C without sizeof by noselasd on Tuesday December 27, 2016 @04:14AM (#13262137)
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 by Asooka on Tuesday December 27, 2016 @02:12AM (#13261731)
I would really love if undefined behaviour meant defined by architecture, but current compiler writers are firmly in the launch nukes camp.
Re:How to find size of an array in C without sizeof by michaelmior on Tuesday December 27, 2016 @01:50AM (#13261685)
I hope not. One of the reasons for allowing undefined behaviour is to allow room for compilers to perform certain optimizations that may not be possible if a specific result were required.
Re:How to find size of an array in C without sizeof by flamedoge on Monday December 26, 2016 @04:22PM (#13259822)
How likely do you run into array bigger than 2gb?
Re:How to find size of an array in C without sizeof by quotemstr on Monday December 26, 2016 @04:35PM (#13259886)
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".

Software is unreliable enough as it is due to problems beneath our notice. It seems reckless to avoid fixing problems that we do notice. Sure, you could argue that rare problems are rare and that users probably won't notice them --- this attitude is penny-wise and pound-foolish, because you can't meaningfully reason about a system that's only probably correct.

Re:How to find size of an array in C without sizeof by Swizec on Monday December 26, 2016 @04:49PM (#13259945)
Engineering is about tradeoffs. How many once-in-a-thousand bugs do you fix before you tackle the one-in-a-million? Or one in a billion? What about if it takes $10/bug to fix every 1:1000 bug and $100,000 to fix one 1:1000000 bug?

Correctness is great in theory, but in practice it's a matter of what's important.

Re:How to find size of an array in C without sizeof by mannykannot on Monday December 26, 2016 @07:44PM (#13260584)
If you are only looking at probability and cost-to-fix you are overlooking something important - the cost if/when it happens.
Re:How to find size of an array in C without sizeof by verdax_1 on Tuesday December 27, 2016 @02:43AM (#13261846)
This is really emphasized in things like dmfea and other failure mode analysis documents or regulated industry. They want you to document the likelihood, your ability to recover from the failure, as well as the cost o the failure. You can say that you didn't want to pay for someone fixing some unlikely fail mode but that's small consultation to the people whose lives your product is ruining.
Re:How to find size of an array in C without sizeof by mjburgess on Monday December 26, 2016 @04:40PM (#13259902)
The problem you're latching on to I think is how the context for caculating a probability can vary.

If it were really as likely as, say, the sun exploding that X happened then it would be of no use to expend time on X.

BUT very often people speaking about the probability of events given suspicious constraints. While a memory allocation might not fail in most situations it will fail often in some situations. And a one-in-a-million chance is almost guaranteed when there are millions of uses.

Re:How to find size of an array in C without sizeof by verdax_1 on Tuesday December 27, 2016 @02:26AM (#13261790)
Also worth considering that our processors are handling billions of ops per second. One in a million might be happening all the time even for one user.
Re:How to find size of an array in C without sizeof by spdionis on Monday December 26, 2016 @04:51PM (#13259957)
That's why it's called one in a million...
Re:How to find size of an array in C without sizeof by maxlybbert on Monday December 26, 2016 @10:25PM (#13261095)
Re:How to find size of an array in C without sizeof by tholmes on Monday December 26, 2016 @07:31PM (#13260533)
One in a million happens quite often if you're processing something like ~100k requests a second.
Re:How to find size of an array in C without sizeof by spdionis on Tuesday December 27, 2016 @04:09AM (#13262112)
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 by jschwartzi on Monday December 26, 2016 @09:03PM (#13260820)
But it's extremely unlikely when you're processing 10 requests a week, such as might be the case for the web server in a consumer-grade router.
Re:How to find size of an array in C without sizeof by quotemstr on Monday December 26, 2016 @09:45PM (#13260953)
It's amazing how skilled blackhats are at converting "rare bug that doesn't affect the UI" into "massive DDoS cannon".
Re:How to find size of an array in C without sizeof by jschwartzi on Tuesday December 27, 2016 @12:52AM (#13261541)
And you can see how risk analyses by senior engineers with tons of embedded experience who are used to working with systems that are not networked leads to problems when their systems are later networked.
Re:How to find size of an array in C without sizeof by paulddraper on Monday December 26, 2016 @09:34PM (#13260918)
...by hundreds of thousands of customers.
Re:How to find size of an array in C without sizeof by chongli on Tuesday December 27, 2016 @10:09AM (#13263513)
One in a million isn't just a typical statement of probability, it's a colloquialism used to refer to things that never happen in practice. It's highly misleading to use in the context of computers which, due to their natures, have one in a million events occurring constantly.
Re:How to find size of an array in C without sizeof by spdionis on Tuesday December 27, 2016 @10:28AM (#13263644)
My comment was tongue in cheek.
Re:How to find size of an array in C without sizeof by Dylan16807 on Monday December 26, 2016 @11:22PM (#13261260)
>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 by verdax_1 on Tuesday December 27, 2016 @02:32AM (#13261808)
I like the way you're thinking, but that sort of thing probably doesn't get past a committee. "Hey we might not be able to think of an application but that doesn't mean our users won't have a legitimate reason for doing it ... Motion passed."
Re:How to find size of an array in C without sizeof by cperciva on Monday December 26, 2016 @04:46PM (#13259924)
A few months ago I was doing FFTs on arrays larger than 4GB. Amusingly, this uncovered a bug in the LLVM optimizer: It was looking at stride lengths to figure out if accesses were independent, and truncated a 4GB stride down to 0.
Re:How to find size of an array in C without sizeof by ksml on Tuesday December 27, 2016 @01:25AM (#13261632)
I would be extremely interested to hear how you found this bug. Sounds like a difficult bug to track down, and I always learn from good debugging stories.
Re:How to find size of an array in C without sizeof by cperciva on Tuesday December 27, 2016 @02:40AM (#13261834)
It was pretty easy to track down: clang38 was exiting with

    Assertion failed: (Distance > 0 && "The distance must be non-zero"),
    function areStridedAccessesIndependent, file /wrkdirs/usr/ports/devel/
    llvm38/work/llvm-3.8.0.src/lib/Analysis/LoopAccessAnalysis.cpp, line 1004.
Looking at the file it was easy to see what was being asserted, and to see that the type was a 32-bit integer; since I knew I was dealing with huge FFTs, the problem was obvious.

Let this be a lesson: Asserting that impossible things don't happen makes debugging much easier when they do happen!

Re:How to find size of an array in C without sizeof by flamedoge on Wednesday December 28, 2016 @01:04AM (#13268760)
Well good thing it was ReleaseWithAsserts build!
Re:How to find size of an array in C without sizeof by vram22 on Monday December 26, 2016 @04:42PM (#13259909)
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 experience, some had bugs in their implementations of binary search that he set them as an exercise. Not sure but I think I remember reading in the article about the Java binary search issue, that even his algorithm had the bug that was found in the Java version. Why it was not found earlier is (maybe) because it only occurred with an extremely large array, IIRC. Don't have a link right now, but it can probably be found by searching for the right phrase.
Re:How to find size of an array in C without sizeof by vram22 on Monday December 26, 2016 @04:48PM (#13259937)
Just did a google search, and it even partially auto-completed this search for me:

bug in java binary search

and showed a related search in the drop-down, 'programming pearls ...', a book by Jon Bentley, which seems to confirm what I said above (though I saw it in his other book, "Efficient Programs", IIRC - he might have mentioned the same issue in the Programming Pearls book too).

Edit: and the Wikipedia article confirms it too:

https://en.wikipedia.org/wiki/Binary_search_algorithm#Implem...

Re:How to find size of an array in C without sizeof by jbn on Monday December 26, 2016 @04:57PM (#13259991)
Re:How to find size of an array in C without sizeof by vram22 on Monday December 26, 2016 @05:21PM (#13260089)
Yes, that's it, by the desc. and dates shown.
Re:How to find size of an array in C without sizeof by pigz on Monday December 26, 2016 @04:53PM (#13259973)
Re:How to find size of an array in C without sizeof by vram22 on Tuesday December 27, 2016 @04:45AM (#13262245)
Yes, thanks, that closely describes the assignment I read that he gave.
Re:How to find size of an array in C without sizeof by loeg on Monday December 26, 2016 @11:10PM (#13261229)
It's basically bogus to have a single object bigger or equal to half of address space (represented by size_t) in C. 32-bit platforms should detect and abort in such conditions (compiler/linker for static objects, malloc() implementation for dynamic allocations).
Re:How to find size of an array in C without sizeof by icebraining on Tuesday December 27, 2016 @05:30AM (#13262383)
Why? If you're running a system with PAE, half of a 32-bit address space is a small fraction of the whole addressable memory.
Re:How to find size of an array in C without sizeof by loeg on Tuesday December 27, 2016 @01:21PM (#13264838)
It's either addressable or it isn't. My understanding of typical PAE systems is that userspace is still limited to 32 bits of address space per process. Any system where userspace is not limited to 32 bits should have a larger than 32-bit size_t. (PAE systems are not true 32-bit platforms.)
Re:How to find size of an array in C without sizeof by dragandj on Monday December 26, 2016 @04:35PM (#13259885)
Today, with ML, big data and similar applications, that might be often.
Re:How to find size of an array in C without sizeof by minipci1321 on Monday December 26, 2016 @04:29PM (#13259855)
Probably not very likely, but keep in mind that this method could also be used without actually allocating the array -- akin to the 'offsetof()' macro. (Which is undefined behavior.)
Re:How to find size of an array in C without sizeof by icedchai on Monday December 26, 2016 @04:46PM (#13259926)
On a 64-bit platform (anything modern), ptrdiff_t is going to be 64-bit so this will not be an issue (ok, 63-bit... but you get my point.)
Re:How to find size of an array in C without sizeof by mannykannot on Monday December 26, 2016 @07:54PM (#13260614)
In this context, the difference between 63 bits and 64 bits is not trivial.
Re:How to find size of an array in C without sizeof by yoklov on Tuesday December 27, 2016 @12:08PM (#13264321)
Both are bigger than the x64 address space...
Re:How to find size of an array in C without sizeof by kabdib on Monday December 26, 2016 @05:40PM (#13260159)
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 by bonzini on Monday December 26, 2016 @05:47PM (#13260188)
The parent is taking about >2gb on a 32-bit machine.
Re:How to find size of an array in C without sizeof by icebraining on Tuesday December 27, 2016 @05:32AM (#13262391)
So what? You can have up to 64GB of RAM on a 32-bit machine: https://en.wikipedia.org/wiki/Physical_Address_Extension
Re:How to find size of an array in C without sizeof by bonzini on Tuesday December 27, 2016 @02:05PM (#13265257)
But those have to be mapped a slice at a time into virtual addresses.
Re:How to find size of an array in C without sizeof by sfifs on Tuesday December 27, 2016 @12:34AM (#13261484)
Oh surprisingly easily. Say you're handling a few billion cookies in RAM or manipulating DNA data.
Re:How to find size of an array in C without sizeof by hsivonen on Tuesday December 27, 2016 @03:35AM (#13261996)
It's quite easy to serve over 2 GB of spaces over the network. (gzip, brotli)