3

Some constructs in programming languages such as C (and derived) have undefined behavior. The term undefined behavior itself is used by the standard of C. The standard itself often writes as

if ... the behavior is undefined.

In StackOverflow, questions with code having undefined behavior are common and a very common response is one of:

  • Undefine behavior
  • Because of undefine behavior
  • Your code invokes undefine behavior

and such. This question is regarding the last choice. Is it correct to say a statement, or a piece of code invokes undefined behavior? Note that the undefined behavior is a result of false assumptions during code compilation, optimization and generation and is not deliberately produced.


While on the subject, what words can be used instead? Surely a piece of code can have undefined behavior. One might also think of cause or contain.

1

I would say no, you cannot invoke undefined behavior. Firstly, this is because in a computing context, invoke has a specific meaning: to execute a specific program or subroutine. You invoke system calls, you invoke procedures and functions, you might even invoke addition, but you wouldn't invoke a machine instruction or a conditional. So it already fails in the sense that it misuses a term with significant technical meaning.

Secondly, invocation implies that the thing invoked has a description or at least a name to call up. You can invoke your authority as a judge to do something, or you can invoke Section 3, paragraph (2)(a) of an ordinance, but you couldn't, say, invoke the law in general. By definition, undefined behavior is only an abstract concept, so a program cannot invoke it. The undefined behavior itself might be to invoke the system shutdown routine, but it's better to say something causes undefined behavior or that the result of something is undefined.

1

Undefined means unspecified is the software world. Undefined behavior can occur, and what is does can be totally unexpected, or the software developer may have taken the liberty to determine and code for a behavior that he expects.

So the code can invoke that behavior. It would be invoking unspecified behavior, which in this sense, is called undefined behavior. Yes, the code can invoke undefined behavior.

4
  • 1
    To be pedantic, undefined behavior and unspecified behavior are separate things in C (if not the whole software world). unspecified behavior is not a bad thing, it's not specified, but whichever the implementation uses is fine. Quoting the standard: An example of unspecified behavior is the order in which the arguments to a function are evaluated. However, undefined behavior is bad. Quoting the standard: An example of undefined behavior is the behavior on integer overflow. C assumes you make sure your integers don't overflow. So if it happens, god knows what might happen! – Shahbaz Mar 19 '14 at 17:44
  • What I mean to say is that, the compiler actively take part in deciding what behavior to generate in case of unspecified behavior, but it is as innocent as you and me with respect to undefined behavior. – Shahbaz Mar 19 '14 at 17:46
  • I've been involved in software development for many years myself, and to my understanding, you may often see an explicit statement that some behavior is undefined, of course. But it still means it is not specified. There may be other behaviors that are implicitly undefined (never mentioned), because they are never explicitly specified. But that's not about English, that's about technical jargon, which should be discussed somewhere else. – Canis Lupus Mar 19 '14 at 17:58
  • surely if a behavior is undefined, it's also not specified, but technically speaking, unspecified behavior means the compiler implementers have to sit down, think about it and choose one behavior that they like best. On the other hand, undefined behavior means the compiler can assume you never write that code. With that assumption in mind, the implementers never actually think what would or should happen in that situation, since that situation is not supposed to happen in the first place. – Shahbaz Mar 20 '14 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.