1

It strikes me as a bit odd that I have heard of loads of undefined behavior examples from C and C++ but not from other Languages, where I also know that C and C++ are used in many situations where reliability is paramount. A search for 'undefined behavior' here on SO yields almost exclusively C and C++ results.

In a course I'm teaching I would like to also give some examples of weird gotchas or undefined behavior from other major languages. Can someone give a few concrete examples similar to int i; if(--i == i++){...} which lead to undefined behavior in other languages?

For example I understand if(--i == i++){...} is undefined in c++ but defined in c# because of extra sequence points as described here Is (--i == i++) an Undefined Behavior?. I want examples of undefined behavior in other languages which are not unforced errors like forgetting to initialize a variable or not locking shared data.

Community
  • 1
  • 1
odinthenerd
  • 5,422
  • 1
  • 32
  • 61
  • 2
    Multi-threaded programs with incorrect synchronization can result in "undefined behavior", in many languages, I suppose. Do you refer to this kind of behaviors? – Eyal Schneider Feb 17 '13 at 22:10
  • 1
    It's an interesting question but it is not really the kind of question for this forum... more likely to invite debate than crisp answers. – Floris Feb 17 '13 at 22:10
  • It's probably because in c/c++ you work so much with pointers and you have to allocate the memory yourself. When you work with a string (char*) in c it's really easy to forget to add the trailing '\0' and that results in undefinded behaviour if you try to print it, while in many languages like python for example you don't have to worry about that. – Ionut Hulub Feb 17 '13 at 22:10
  • @Floris I edited my question to be answerable in a concise way, namely an example of odd syntax in a non C or C++ language which leads to undefined behavior. – odinthenerd Feb 17 '13 at 22:30
  • 1
    @PorkyBrain - The major difference is that the C and C++ standards take care to explicitly point out some cases that are undefined behavior. The C++ standard also says *"Undefined behavior may be expected when this International Standard omits any explicit definition of behavior"*, which is the only thing many other language definitions do. – Bo Persson Feb 17 '13 at 23:25
  • @BoPersson thank you very much for pointing that out! so what your saying is that C and C++ just do a better job of defining undefined behavior? That sounds plausible. – odinthenerd Feb 17 '13 at 23:28

1 Answers1

3

The C and C++ language expect the programmer to do the hard work - this means there is no bounds checking, etc. The advantage of this is speed - if you know you are not going to write past the ends of your array (your algorithm forbids it), there is no need to check in every iteration. Many high level languages build in lots of safeguards - they will automatically assign variables when they are first declared, expand an array if you assign outside the current bounds, keep track of the length of a string for you, ...

This can lead to problems too - if you don't have to declare a variable, then a mistyped variable can lead to a hard-to-spot bug. This is why e.g. Visual Basic has the Option Explicit statement which overrides the default behavior and forces the user to declare every variable - catching many bugs along the way. The same thing (not declaring variables) can lead to unexpected scope issues - not the same thing as "undefined" behavior, but "unexpected".

In languages with "nice, easy" array manipulation - Python or Perl, for example, - you can run into an interesting (and, I think, undefined) behavior when you loop over an array whose contents you are changing:

for tup in somelist:
    if determine(tup):
         code_to_remove_tup

(in this example, looping over a list and removing tuples that meet certain criteria - from Remove items from a list while iterating)

When an array is either growing or shrinking during a foreach loop, all bets are off (and some interpreters will thrown an exception). Once again - it is "easy" to do the wrong thing, and get an unexpected result...

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122