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?
[ Parent
| on HN ]
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.