Monday, 15 August 2011

c# - Examples of undefined behavior in languages other than C and C++ -



c# - Examples of undefined behavior in languages other than C and C++ -

it strikes me bit odd have heard of loads of undefined behavior examples c , c++ not other languages, know c , c++ used in many situations reliability paramount. search 'undefined behavior' here on yields exclusively c , c++ results.

in course of study i'm teaching give examples of weird gotchas or undefined behavior other major languages. can give few concrete examples similar int i; if(--i == i++){...} lead undefined behavior in other languages?

for illustration understand if(--i == i++){...} undefined in c++ defined in c# because of sequence points described here is (--i == i++) undefined behavior?. want examples of undefined behavior in other languages not unforced errors forgetting initialize variable or not locking shared data.

the c , c++ language expect programmer hard work - means there no bounds checking, etc. advantage of speed - if know not going write past ends of array (your algorithm forbids it), there no need check in every iteration. many high level languages build in lots of safeguards - automatically assign variables when first declared, expand array if assign outside current bounds, maintain track of length of string you, ...

this can lead problems - if don't have declare variable, mistyped variable can lead hard-to-spot bug. why e.g. visual basic has option explicit statement overrides default behavior , forces user declare every variable - catching many bugs along way. same thing (not declaring variables) can lead unexpected scope issues - not same thing "undefined" behavior, "unexpected".

in languages "nice, easy" array manipulation - python or perl, example, - can run interesting (and, think, undefined) behavior when loop on array contents changing:

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

(in example, looping on list , removing tuples meet criteria - remove items list while iterating in python)

when array either growing or shrinking during foreach loop, bets off (and interpreters thrown exception). 1 time 1 time again - "easy" wrong thing, , unexpected result...

c# java php language-agnostic undefined-behavior

No comments:

Post a Comment