6 ms·
> The code is undefined, but "I don't know." is still the correct answer for what happens to the variable. Well, then a better choice would be "I can't know."
by pksadiq 7y ago
> The code is undefined, but "I don't know." is still the correct answer for what happens to the variable.
Well, then a better choice would be "I can't know."
- Filligree 7y agoCompile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent.
- thedufer 7y agoWith what compiler? Targeting what architecture? The question is underspecified. "I can't know" is correct.
- Gibbon1 7y agoQ: What is a leaky partial abstraction of the C standard? Ans: A compiler.
- mikeash 7y agoAnd then you update your compiler and something completely different happens.
- saagarjha 7y agoOr your standards conforming but mischievous compiler does something nondeterministic ;)
- owl57 7y ago1) This made me curious. Are any of the compilers in real use nondeterministic? 2) Probably that's not needed? A normal optimizing compiler just inlines the function somewhere new — and boom? Then again, can that really happen with practical contemporary compilers and this exact statement?
- mikeash 7y agoMost compilers are nondeterministic in small ways. For example, it's common to use hash tables that are keyed by pointer address and then iterate over the entries in storage order, so the order in which certain things are emitted will change from run to run. This is why "deterministic builds" are such a big deal, and not just an obvious thing that you get for free. I don't know what the chances are that such a thing could ever translate into good assembly being emitted in one run and bad assembly being emitted in the next.
- Dylan16807 7y agoRegister allocation can be quite tricky, and sometimes it can only explore a small part of the problem space, so if you don't start the algorithm with exactly the same seed you might end up with significantly different code in certain functions.
- AdieuToLogic 7y ago> Compile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent. This pretty much is the definition of "undefined behaviour" in the context of a standardized language specification.
- amluto 7y agoWhoa there! You mean “unspecified behavior”. int i = [unspecified] means that i has some value, but the spec doesn’t determine the value. Undefined behavior means that all your secrets might be sold to the highest bidder, your centrifuges might explode, and your computer is now full of ransomware.
- AdieuToLogic 7y ago> Whoa there! You mean “unspecified behavior”. int i = [unspecified] means that i has some value, but the spec doesn’t determine the value. Undefined behavior means that all your secrets might be sold to the highest bidder, your centrifuges might explode, and your computer is now full of ransomware. That's only when using Boehm GC[0] in kernel device drivers that self-modify. Or any MSVC binary. :-D 0 - https://www.hboehm.info/gc/ https://www.hboehm.info/gc/
- AdieuToLogic 7y agoWhilst my other comment was intended to be jovial, it is hard to say if that was accurately conveyed. So this one will be serious. The original problem definition, as specified by @pksadiq, read thusly: > Say for example, in question 5, the statement "return i++ + ++i;" is undefined ... This inspired a response by @Filligree of: > Compile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent. Given the original constraint of an undefined statement result, and the suggested activity to address same, I posited that the recommended action is an exemplar of observing the product of undefined behaviour. You then contributed: > You mean “unspecified behavior”. As per c-faq.com[0], there are three categories identified relating to this topic: 1 - implementation-defined: The implementation must pick some behavior; it may not fail to compile the program. 2 - unspecified: Like implementation-defined, except that the choice need not be documented. 3 - undefined: Anything at all can happen; the Standard imposes no requirements. Whereas you imply a standards-conformant implementation of "return i++ + ++i;" is unspecified (category #2), it is, in fact, undefined (category #3). The support for this assertion is as follows. As per the same site, Question 3.8[1] includes: > Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. And further states: > ... if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written. This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification. And concludes with an example stating: > ... the Standard declares that it is undefined, and that portable programs simply must not use such constructs. Therefore, the original expression presented by @pksadiq is in fact an exemplar of an undefined expression as defined by category #3 shown above. Since both it and the message to which I originally responded satisfy same, I stand by my response given to @Filligree as having had informally defined the standard C concept of "undefined behaviour." 0 - http://c-faq.com/ansi/undef.html http://c-faq.com/ansi/undef.html 1 - http://c-faq.com/expr/seqpoints.html http://c-faq.com/expr/seqpoints.html
- leni536 7y agoUB includes the code not compiling, although it rarely happens in practice.
- bandushrew 7y ago"I cant know" is a subset of "I dont know" "I dont know" was absolutely the correct answer.
- ineedasername 7y agoExcept from the perspective of a pragmatics linguistic analysis, "I don't know" has a social context of "There's an answer, and I don't know it." In this case, a non-C programmer should answer "I don't know" to all of them. A person with a passing familiarity should answer similarly. A seasoned pro would be forced to answer the same. Making it a rather useless tool for distinguishing people who think they know C but are honest when faced with their limitations or those who truly know it and know the answer is undetermined, which is supposed to be the point of the exercise.
- Thorrez 7y ago>which is supposed to be the point of the exercise. I don't think that assumption is justified. Someone could say the point of the exercise is to illustrate that C is confusing.
- deleted 7y ago[deleted]
- ineedasername 7y agoYou don't think the claim is justified because some might say otherwise. Some is irrelevant. Some might say lots of mutually exclusive interpretations. It's the author's intent that matters, and the context of the Author's post indicates the some interpretation isn't the author's intent. His post begins with the question "So you think you know C?". He then goes on to present a test that is, by his own words, intended identify to test takers whether or not they really understand the intricacies of C, and to think critically about that source of their knowledge "I had to learn to rely on the standard instead of folklore; to trust measurements and not presumptions; to take “things that simply work” skeptically" Never once does the author mention that C is confusing, use the word confusing, or otherwise indicate that general idea. If you're getting that impression, it's your own reading into it. I'm not even saying you'd be incorrect, but that's not the author's intent, which was the basis of my comment.
- deleted 7y ago[deleted]