Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • How did you manage to detect undefined behaviour, pray tell? Commented Sep 11, 2011 at 17:59
  • BTW it's not just about "other compilers, or maybe even other versions of the same compiler", but even different runs with the same compiler; or perhaps it only appeared to work in your case but in fact didn't at all and instead created a finger-sized hole in the universe..? Commented Sep 11, 2011 at 18:00
  • @Tomalak I printf()'d the contents of the array. that's a very reliable way to detect UD imo. And I was only stating experimental results, without any claim of completeness. If you find an example of g++ creating an executable where the declaration of the question above does not produce the intended results, let me know :) - My guess is, that the g++ detects initializations of this kind and handles them appropriately. But, as I said, I wouldn't rely on it, and would most certainly never use it. Commented Sep 11, 2011 at 18:31
  • It is mathematically impossible, in many cases, to "detect" UB. Certainly, printf-ing something is never a UB detection tool. Commented Sep 11, 2011 at 18:37
  • well, of course exact detection is generally not possible. But the case we have here is pretty simple, in a way that allows to identify undefined behaviour if something went wrong and all other possible sources for "something wrong" have been eliminated. The probability that it's not UB when something went wrong is insignificant, and can thus be eliminated almost completely by running every test at least twice. Commented Sep 11, 2011 at 18:43