5

Recently I came across a number of undefined features in C, one of them being the following:

https://stackoverflow.com/questions/8698048/behaviour-of-non-const-int-pointer-on-a-const-int

Could someone tell me what should I answer when such a question is asked during online exams ( and None of These / Undefined option is not available ) ?

Also, could you point me to a document specifying the most commonly encountered undefined behaviours in C ?

Thanks.

SHOUBHIK BOSE
  • 381
  • 2
  • 8
  • 2
    Just write "this is undefined" next to it. The document containing all UBs is the C standard. –  Jan 02 '12 at 07:40
  • Link to the doc ?@WTP – SHOUBHIK BOSE Jan 02 '12 at 07:42
  • I guess it is http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=57853 –  Jan 02 '12 at 07:49
  • oh, well. is an option regarding [nasal demons](http://catb.org/jargon/html/N/nasal-demons.html) available? – ZJR Jan 02 '12 at 09:33
  • Could you list the options you saw in that exam? We may organize a massive write-in campaign to the exam publisher. – rwong Jan 02 '12 at 15:29
  • May be a related post - https://softwareengineering.stackexchange.com/questions/398703/why-does-c-have-undefined-behaviour-and-other-languages-like-c-or-java-don – Sisir Sep 22 '19 at 17:59

2 Answers2

10

You can do either of the following:

  1. Check the "right" answer (the one that your experience with the professor tells you that he wants to see). Sometimes the professors think they know better when they don't. You can argue (and get a hit with the grade) or just let them hear what they want to hear.

  2. Approach the professor and raise the issue. Ask a question during the office hours or even in the class. That's what I would do. You don't want to know my GPA.

  3. Write (if possible) an additional answer "This is undefined behavior per standard, but I believe most compilers would do ..." and proceed with #1.

Generally if professors put you in this position it means a lot about their own knowledge and qualifications. But, I don't think you want to put yourself into a position of that annoying student that puts the professor to shame. I did it twice, in one class I ended up with A+, in another with barely a B (after scoring 100% in the final exam). You never know how they'll take it. Some are ready to admit a mistake, some will be very vicious.

littleadv
  • 4,704
  • 27
  • 26
3

I presume you are referring to an online exam in which there is no professor to speak to, and no margin to handwrite an objection.

So, what I would do in your shoes is that I would pick the answer that most closely matches "undefined" (something like "prohibited" or "crash") and if none of these are available then I would just pick a guess. Then, I would wait until my exam gets graded, and if my answer to this question is flagged as wrong, I would take it to the dean.

Start looking for the dean's email from now.

EDIT

The most famous instance of undefined behavior in C is this:

int somefunction( int x, int y );
...
int a = 5;
int result = somefunction( a, a++ );

The question is "what is wrong with the above call?"

I came across this one in an interview once, but it was just for bonus points, they did not really expect the candidate to know. The answer is undefined behavior because C does not guarantee to you that 'a++' will be evaluated after 'a'. So, somefunction may be called with x=5, y=6 or it may be called with x=5, y=5 or x=6, y=6. (or a whale and a bowl of petunias might fall from the sky <-- EDIT: no, this is not a possibility.)

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111
  • Cool, very often one encounters the printf("%d %d",a,a++); similar to what you were asked. – SHOUBHIK BOSE Jan 02 '12 at 09:41
  • Is that really undefined behavior in the sense of demons flying out of your nose? According to my (somewhat limited) it's simply unspecified whether the above code will call `someFunction(5,5)` or `someFunction(6,5)` because the order in which arguments are evaluated is unspecified. But it's still guaranteed that it won't do anything else (as opposed to `somefunction(a++, a++)`, which might do anything it damn well pleases (on account of modifying the same variable twice without a sequence point in between invoking undefined behavior)). – sepp2k Jan 02 '12 at 11:35
  • Good point. It depends on what we mean by "undefined behavior". I think "undefined behavior" is when we have an operation whose outcome cannot be deterministically guaranteed by the standard, and the standard chooses to expressly warn against it. But I suppose that this question can be answered by reading the standard, so there is no point in theorizing about it. – Mike Nakis Jan 02 '12 at 12:57
  • @Mike: Fair enough, I just did. The C99 standard (or the draft of it that I got a hold of at least) defines undefined behavior to be when the standard "imposes no requirement" on the behavior (section 3.4.3), i.e. it might do anything at all. It defines unspecified behavior to be when the behavior will be one out of multiple defined possibilities. I.e. it is not defined whether value a or value b is used, but it is defined that one of them will be used. It actually cites the order in which function arguments are evaluated as an example of this (section 3.4.4). – sepp2k Jan 02 '12 at 14:00
  • @sepp2k I see. So, the example at hand is undefined behavior, it is just not nasal demons nor whales and petunias. – Mike Nakis Jan 02 '12 at 14:20
  • Actually I was wrong. Turns out this is undefined behavior after all, but not because the order of argument evaluation is undefined. It's undefined behavior to read and modify the same value without a sequence point in between unless the modification depends on the value of the read (6.5.2). So the code can in fact make a whale fall out of the sky (while something like `somefunction(printf("foo"), printf("bar"))` could not - that could only print foobar or barfoo). – sepp2k Jan 02 '12 at 14:37
  • @sepp2k q(o_O)p actually, I do not understand what they mean when they say `unless the modification depends on the value of the read`. `a++` is a modification which depends on the value of `a`. So, perhaps my example was incorrect? – Mike Nakis Jan 02 '12 at 14:44
  • 1
    @MikeNakis The exact wording is "Furthermore, the prior value shall be read only to determine the value to be stored", which I take to mean that `x` may only be used "inside" the expression that modifies `x`. I.e. `f(x += x)` is fine because here the modification depends on `x` and thus its unambiguous that `x` must refer to the old value here. But `f(x += y, x)` is not fine because here the modification does not depend on the value that is used as the second argument. – sepp2k Jan 02 '12 at 14:50
  • @sepp2k OK, (I think) I understand. So, I will leave it as it is. – Mike Nakis Jan 02 '12 at 15:14
  • 1
    Why did you strike out the whales and petunias? Given the code `if (conditionThatWillNeverActuallyArise) DropWhalesAndPetunias(); someFunction(a,a++);` a compiler could infer that because the program execution cannot possibly be defined if the above code is reached when `conditionThatWillNeverActuallyArise` is false, and might be defined if it's true (e.g. if `DropWhalesAndPetunias` calls `exit`), the call to `DropWhalesAndPetunias()` may as well be made unconditional. – supercat Aug 01 '15 at 20:26