3

In this question, OP intentionally calls a C standard library function with invalid argument types and tries to predict what is going to happen. OPs makes some assumptions about where the arguments to a function are drawn from and asks why his assumptions don't seem to hold.

Two undeleted answers existed. The first answer explains that what OP does is undefined behaviour and that no assumptions can be made. At the time of writing, this answer stays at a score of 6.

The second answer (written by me) explains why OPs assumptions don't hold and gives a basic introduction into the relevant calling convention. It does not explicitly state that what OP attempts to do is undefined behaviour. This answer stays at a score of −4 at the time of writing.

For me, it is clear that OP is aware of the undefined behaviour in his question. I see OP trying to understand what is going on internally by deliberately doing undefined things. In my opinion, this is a great way of learning how things work. Should we discourage such questions by striking them with downvotes and “this is undefined behaviour, no assumptions can be made” boilerplate or should we attempt to give an educative explanation of where OPs observations come from?

Should we explicitly point out the undefined behaviour even though the question makes reasonably clear that OP does not expect behaviour to be well-defined?

37
  • 9
    What in the question gives you the impression that the asker knows this is undefined behavior? He says he is trying to predict the output, and doesn't understand why it isn't working the expected way. An answer that fails to mention this is undefined behavior is a bad answer because it is incomplete. You are not going to learn much of anything useful by "deliberately doing undefined things."
    – Cody Gray Mod
    Commented Mar 8, 2016 at 10:48
  • 2
    @CodyGray I beg to disagree. I'm not a nanny who needs to point out undefined behaviour to experienced C programmers. A lot of things can be understood by deliberately doing undefined things and OP is doing exactly that. He has a basic understanding of calling conventions and now a new portal to the complex concept of the amd64 sysv ABI opens up to him.
    – fuz
    Commented Mar 8, 2016 at 10:50
  • 3
    It's a bit off to ask the community for their opinion, and then argue the toss when the community gives it. It's clear what your opinion is. Why not sit back and let the community respond? Commented Mar 8, 2016 at 10:52
  • 3
    It is not a nanny thing, it is an answer thing. An answer about the C language that ignores the language standard is a bad answer. No different than an answer that proposes invalid syntax. You seem to be confusing "undefined behavior" with "best practices." I might be sympathetic with you if this was an issue where people were downvoting your answer because it advocated something that was not seen as a "best practice," but best practices are nebulous at best. The language standard is pretty clear on UB, and if you intentionally write code invoking UB, that is bad code, objectively speaking.
    – Cody Gray Mod
    Commented Mar 8, 2016 at 10:53
  • 2
    Someone who does not know that sharp objects hurt can still deliberately touch a sharp object and be surprised when they get hurt. Just because someone is deliberately doing something that is considered UB doesn't mean that they realize they're invoking UB.
    – BoltClock Mod
    Commented Mar 8, 2016 at 10:53
  • 3
    Perhaps the most distressing aspect to this is that the asker of the original question describes himself as Software Engineer @ Microsoft!! ;-) Commented Mar 8, 2016 at 10:56
  • 1
    @CodyGray So we are not allowed to answer questions about dlsym() either as casting a void* to a void(*)() is a constraint fault? So we are not allowed to answer questions about gcc extensions either? About the inter-operating between C and C++? You are being hypocritical in my opinion. It is nowhere said that OP wants to do this in production code. It is pretty clear that OPs code is an experiment. Is it verboten to write intentionally bad/undefined code to study what it does?
    – fuz
    Commented Mar 8, 2016 at 10:56
  • 6
    @FUZxxl Answering those questions is fine. But: First, give the "proper" answer, which is all anyone should ever rely on. Then, you can dissect how one specific fully named implementation reacts in that specific circumstance, and why it worked out that way. Such an answer can be highly educational, though only for hacking, not for any sane coding. Regarding dlsym: The point you have to make there is that while the C standard does not handle that, the additional standards the implementations in question follow (name them) define that behavior. Commented Mar 8, 2016 at 11:02
  • 3
    @FUZxxl Nothing in the original question gives off the impression that OP knows it is UB. He actually added the compiler version and environment information only when asked for. Many people have a sense of how computers work and might make the wrong assumption that C is a one-to-one mapping of that. Commented Mar 8, 2016 at 11:10
  • 2
    @FUZxxl you have all the knowledge to make this into an exemplary answer, by stating that the behaviour is undefined, and you perhaps shouldn't be relying on any specifics unless you're an author of libffi or a compiler, and then dissecting this particular case. I only have power over whatever I vote, which answers to give bounties, and which answers to link forward, but if "internet points" are of any importance to you, you'd notice that the community very well stated why they were downvoting your answer whilst upvoting the other, general (but perhaps not so thorough) answer. Commented Mar 8, 2016 at 11:21
  • 2
    ..same with all the other 'I did a bad thing, explain fully and clearly why bad things happened'. questions. Every day, 'I used %d on a float and got 0.0 - explain why'. NO. If you want, YOU find out and tell us. Commented Mar 8, 2016 at 17:06
  • 2
    Especially given the very recent title update, it seems like an interesting and answerable question to me. On the whole, I agree with your comment under your deleted answer: "Undefined by the standard. That doesn't mean that you can never predict what's going to happen." Obviously there is some behavior that actually happens, and it can be instructive to see what that behavior is and especially why. My only concern is that it does take a lot of effort to answer well: this seems like it calls for another "branch prediction"-type essay. But I could be wrong.
    – jscs
    Commented Mar 8, 2016 at 19:14
  • 4
    The problem with UB questions (Why is i++ + ++i undefined behavior?) is that, once you recognize it as UB, the question becomes completely uninteresting to anyone except compiler writers, demented students and nutty professors. Nobody in their right mind would ever write code like this, and you can't rely on the compiler to produce a sensible result, so discussing it beyond identifying it as UB and explaining why is completely pointless.
    – Robert Harvey Mod
    Commented Mar 9, 2016 at 2:11
  • 4
    @JoshCaswell: Straw man.
    – Robert Harvey Mod
    Commented Mar 9, 2016 at 2:19
  • 2
    @FUZxxl: I stopped listening after "If you can't provide an answer, move on." You could have saved me some time by leading with that, instead of trying to set me straight and then saying "don't bother to reply."
    – Robert Harvey Mod
    Commented Mar 9, 2016 at 2:50

3 Answers 3

16

You can do whatever you want—it is your answer. By the same token, the people who read your answer can also vote on it however they want—it's their vote.

So the whole spirit of this Meta question is a little bit specious to me. Are you honestly trying to figure out if your answer is good/useful? If so, I think you can let the community answer that one. Not the Meta community, of course, but the community. They have spoken by voting on your answer, and they will continue to speak. At this point, it is fairly clear that they consider it to be not an especially useful answer.

The only other option is that you're running to Meta, hoping to get some sympathy for your answer being downvoted. Sorry, I have no sympathy for that. I wouldn't be particularly sympathetic even if you had posted the best answer of the day, because votes are individual decisions and I'm not here to second-guess the people who cast them. But I'm especially unsympathetic in this case, because I agree that this is a bad answer.

Quoting myself:

What in the question gives you the impression that the asker knows this is undefined behavior? He says he is trying to predict the output, and doesn't understand why it isn't working the expected way. An answer that fails to mention this is undefined behavior is a bad answer because it is incomplete. You are not going to learn much of anything useful by "deliberately doing undefined things." … An answer about the C language that ignores the language standard is a bad answer.

You indicate, both in the comments and in an update to the question, your belief that the question makes it "reasonably clear" the asker understands what he is doing is undefined behavior. Two points:

  1. Even if that were true, answers are intended for more than just the person who asks the question. Their arguably most important target audience are the millions of users who arrive at Stack Overflow looking for an answer to a similar question. Even if this guy happens to be a genius on the language committee who wrote the spec for printf and contributed its implementation to GCC, the odds are that people who arrive at this question in the future will not be as knowledgeable. Therefore, if your answer fails to cover one of the most important pieces of information pertinent to the question, it is not a good answer and not likely to be useful.

    If you're right about this, the asker may even accept your answer, while the community simultaneously downvotes it. It is a bad answer to the question in general, but a helpful answer to the person who asked the question. Again, as I mentioned at the top, it is your answer to post. Leave it, improve it, delete it, whatever you want to do with it. There is no "general consensus" to be established here, outside of what the C community has already spoken clearly on.

  2. I very much disagree that the question provides any indication that the person knows this is undefined behavior. Your reading is far too generous. He says that "the output seems random on my computer...I try to predict the output by...". Statements like this are generally not made by people who understand that undefined behavior is and what it means. He goes on to say that "I expected its print some value near &a"—wait, what? Isn't that just saying that he expected the behavior to be defined in a certain way, but found out that it is not defined?

    It also seems quite unlikely to me that a person who knows this is undefined behavior would even be asking this question. Good programmers don't write code that exhibits undefined behavior and then try to figure out why it is behaving in that way. Upon learning that their code exhibits undefined behavior, they fix their code, rewriting it so that it is in compliance with the language standard. An answer that glosses over this is not only wrong—it is not even wrong.

    It's hard to settle this debate about the author's intent, of course, since neither of us are inside his head. But even if we were to get confirmation from zhiwenf himself that he understands what undefined behavior is, it wouldn't make your answer useful in the larger sense, because future Googlers wouldn't learn anything about the undefined behavior either by reading the question or by reading your answer.

7
  • 8
    Good programmers don't write code that exhibits undefined behavior and then try to figure out why it is behaving in that way - I must absolutely disagree with this. Trying to learn something new, or testing things out doesn't make someone a bad programmer - quite the opposite, in fact.
    – Rob Mod
    Commented Mar 8, 2016 at 11:36
  • 8
    I didn't say that "trying to learn something new" or "testing things out" makes someone a bad programmer. Far from it. I said that good programmers don't write code that exhibits undefined behavior on purpose without a really good reason. There is very little that you can learn from studying the behavior of code exhibiting undefined behavior, except perhaps understanding some of the quirks/implementation-details of the compiler. You would be far better off reading the compiler documentation to get that information. @rob
    – Cody Gray Mod
    Commented Mar 8, 2016 at 11:40
  • 3
    @Rob: The problem with that approach is that you have to really understand optimizing compilers. Once you go off the rails, so will they, and the result may be impossible to explain without understanding the compiler internals. For instance, calling a function with invalid arguments will tell todays compilers that the call is in fact unreachable, and quite a bit of code around it can then be transformed.
    – MSalters
    Commented Mar 8, 2016 at 14:42
  • 4
    @Rob: I want to add to what Code Gray said: "There is very little that you can learn from studying the behavior of code exhibiting undefined behavior, except perhaps understanding some of the quirks/implementation-details of the compiler." That is perhaps the most dangerous thing about trying to learn purely from undefined behavior. You think that you have some idea of what's going on, but you really don't. What you have is an incomplete understanding based on an arbitrary decision made by compiler developers that can be changed in the future. "Learning" this way is highly dangerous. Commented Mar 8, 2016 at 16:30
  • If some dev wishes to blow themselves up by trying to defuse UB, fine. What I can't support is posting on SO, asking about detonator removal, instructions on using wire-cutters and which colour wire to cut:( Commented Mar 8, 2016 at 17:04
  • I have updated the question, You will find why I ask this question.
    – notbad
    Commented Mar 8, 2016 at 21:12
  • 1
    +1 for the first point. That is probably the most relevant point - the answer will be read by people who aren't the OP and any assumption about the OP may not apply to them.
    – Matthew
    Commented Mar 9, 2016 at 5:21
1

No big deal here.

About the question

I followed this question from the first very first time it was posted. And OP did NOT aware of Undefined Behavior and automatic variable address problem (I believe). That's why he got the downvotes and my answer got upvotes (because I pointed them out) at that time.

After some answers and comments occurred, OP change the content of question to show that he does aware of UB. The current question now is "can we predict the outcome of printf's undefined behavior ?", which is a good question but make the answers and comments below become weird.

About your answer

The reason you got downvotes is that you didn't point out the UB case in the question at that time.

You was a bit uncomfortable about the downvotes but I think you shouldn't. There are some downvotes just means that some guys feel your answer not useful, it does not mean you answer not useful. If you believe in your point, maintain it so future people could make upvotes if they find it useful, that the way Copernic did.

How should we deal with (C) language questions that operate outside of the realm of the language specification?

I believe SO users do encourage this kind of question, there is no reason to reject it. But the question is not about that.

2
  • 1
    OPs original question was pretty clear in that OP did know that this was undefined behaviour. If OP didn't knew, he would surely have asked why the output is random instead of taking it for granted. What leads you to think that OP did not know that what he attempts to do is undefined behaviour?
    – fuz
    Commented Mar 9, 2016 at 10:52
  • And yes, this question is about stuff that is outside the realm of ISO 9899. Or do the words “calling convention” and “stack” appear in the standard?
    – fuz
    Commented Mar 9, 2016 at 10:53
1

The experimentation being asked abut here -- trying to figure out what a questionable or downright undefined construct does, sometimes as part of a misguided attempt to salvage some degree of definedness from an undefined situation -- is a very, very natural thing to do. Humans are born experimenters. Most beginning programmers dabble in it. Many textbooks -- including the venerable K&R -- explicitly encourage it. I'd bet that most of the regulars howling about how dangerous it is to engage in undefined behavior actually engaged in quite a bit of it back when they were learning, too.

We can warn about the dangers of undefined behavior, we can remind people not to learn the wrong lessons from their experiments with it, but we can no more stop people from doing it than we can stop hikers from climbing mountains (or college students from drinking alcohol).

Downvoting, or otherwise being too shrill in our condemnation of undefined behavior, will eventually make us look like a bunch of fuddy-duddies not to be regarded. We're supposed to be working with questioners as peers, not as overprotective parents reminding vulnerable five-year-olds not to cross the street alone or accept candy from strangers.

1
  • 2
    We can't stop people from experimenting, but that doesn't mean that such experiments are appropriate, or useful, as SO questions. There are lots of things that people do that aren't appropriate to ask about in SO questions, even within the realm of programming problems. People downvoting an answer aren't saying that no one should experiment, ever, they're just saying that they don't feel that that question, or that observation, is useful information that belongs on this site.
    – Servy
    Commented Mar 9, 2016 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .